Date and SimpleDateFormat classes, Calendar classes

Date and SimpleDateFormat classes

The object created using the default no-parameter constructor of the Date class represents the current time. We can directly output the Date object to display the current time. The display format is: Mon May 07 15:05:36 CST 2018.
You can use SimpleDateFormat to format a date and time, such as converting a date to text in a specified format, or converting text to a date.
The parse() method can convert text to date. (A conversion exception, namely ParseException, may occur when calling the parse() method of the SimpleDateFormat object, so exception handling is required. When using the Date class, you need to import the java.util package, and when using SimpleDateFormat, you need to import the java.text package)

public class HelloWorld {
    public static void main(String[] args) throws ParseException {

        // 使用format()方法将日期转换为指定格式的文本
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 创建Date对象,表示当前时间
        Date now = new Date();

        // 调用format()方法,将日期转换为字符串并输出
        System.out.println(sdf1.format(now));
        System.out.println(sdf2.format(now));
        System.out.println(sdf3.format(now));

        // 使用parse()方法将文本转换为日期
        String d = "2018-5-7 15:05:36";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 调用parse()方法,将字符串转换为日期
        Date date = sdf.parse(d);

        System.out.println(date);
结果为:
201805071505032018/05/07 15:05
2018-05-07 15:05:03
Mon May 07 15:05:36 CST 2018

The Calendar class handles time and date processing.

The java.util.Calendar class is an abstract class. You can obtain a Calendar object by calling the getInstance() static method. This object has been initialized by the current date and time, that is, the default represents the current time, such as Calendar c = Calendar.getInstance();
call The getInstance() method of the Calendar class obtains an instance, and then the date and time information is obtained by calling the get() method. The parameter is the value of the field to be obtained, and Calendar.Year is the static constant defined in the Calendar class.
The Calendar class provides the getTime() method to obtain the Date object and complete the conversion between Calendar and Date. You can also obtain the time value of this Calendar in milliseconds through the getTimeInMillis() method.

Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR); // 获取年
        int month = c.get(Calendar.MONTH) + 1; // 获取月份,0表示1月份
        int day = c.get(Calendar.DAY_OF_MONTH); // 获取日期
        int hour = c.get(Calendar.HOUR_OF_DAY); // 获取小时
        int minute = c.get(Calendar.MINUTE); // 获取分钟
        int second = c.get(Calendar.SECOND); // 获取秒

        System.out.println("当前时间:" + year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second);

        Date date = c.getTime(); // 将Calendar对象转换为Date对象
        Long time = c.getTimeInMillis(); // 获取当前毫秒数
        System.out.println("当前时间:" + date);
        System.out.println("当前毫秒数:" + time);
运行结果:
当前时间:2018-5-7 15:21:24
当前时间:Mon May 07 15:21:24 CST 2018
当前毫秒数:1525677684464

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325796448&siteId=291194637