1 package com.web.test;
2
3 import java.text.ParseException;
4 import java.text.SimpleDateFormat;
5 import java.util.Calendar;
6
7 public class DateUtil {
8
9 public static void main(String[] args) throws ParseException {
10 System.out.println(getNowTime());
11 System.out.println(getHourAndMinute());
12 System.out.println(getNowDate());
13 }
14
15 /**
16 * 获得当前时间
17 * @return 字符串,当前时间
18 */
19 public static String getNowTime(){
20 Calendar cal=Calendar.getInstance();
21 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
22 return sdf.format(cal.getTime());
23 }
24
25 /**
26 * 获得当前小时和分钟
27 * @return 字符串,小时和分钟
28 */
29 public static String getHourAndMinute(){
30 String nowDate = getNowTime();
31 return nowDate.substring(11, 16);
32 }
33
34 /**
35 * 获得当前日期
36 * @return
37 */
38 public static String getNowDate(){
39 return getNowTime().substring(0, 10);
40 }
41
42 }