Home  >  Article  >  Java  >  Description of common date classes in Java

Description of common date classes in Java

伊谢尔伦
伊谢尔伦Original
2017-04-29 13:01:481666browse

There are three commonly used date classes, Date class, Calendar (calendar) class and date format conversion class (DateFormat)

Most of the methods in the Date class are obsolete, and generally only the constructor is used Method to obtain the current time of the system.

public class DateDemo {  
    public static void main(String[] args) {  
        Date date = new Date();  
        System.out.println(date);  
    }  
}

The result output is the current system time: Fri Mar 10 16:50:37 CST 2017
We can see that we are not used to this format of time, so when displaying the time The output format must be converted. At this time we need to use the date format conversion class DateFormat.

public class FormatDemo {  
    public static void main(String[] args) {  
    Date d=new Date();  
    System.out.println(d);  
    Format f=new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");  
    String s=f.format(d);  
    System.out.println(s);  
    }  
}

The output time at this time is: 2017-03-10 04-54-06

This looks very comfortable.

Calendar

The Calendar class is an abstract class that associates a specific moment with a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, etc. Methods are provided for converting between and for manipulating calendar fields (such as getting next week's date).

You can use three methods to change calendar fields: set(), add() and roll().

1, set(f, value) changes calendar field f to value.

2, add(f, delta) adds delta to the f field.

3, roll(f, delta) adds delta to the f field, but does not change the larger field.

public class Test {  
    public static void main(String[] args) {  
    Calendar c=new GregorianCalendar();  
    c.set(Calendar.DAY_OF_MONTH,1);  
    System.out.println("输出的是本月第一天");  
    System.out.println((c.get(Calendar.MARCH)+1)+"月的"+c.get(Calendar.DAY_OF_MONTH)+"号");  
    c.roll(Calendar.DAY_OF_MONTH,-1);  
    System.out.println("输出的是本月最后一天");  
    System.out.println((c.get(Calendar.MARCH)+1)+"月的"+c.get(Calendar.DAY_OF_MONTH)+"号");  
    }  
}

The output result is:

The output is the first day of this month

The 1st of March

The output is the last day of this month

The 31st of March

During the operation of the Roll method, after the number of days on the first day is decremented by one, it directly returns to the last day of this month. The date changes cycle within this month without to change the month, i.e. larger fields will not be changed.

Compare add method:

public class Test {  
    public static void main(String[] args) {  
    Calendar c=new GregorianCalendar();  
    c.set(Calendar.DAY_OF_MONTH,1);  
    System.out.println("输出的是本月第一天");  
    System.out.println((c.get(Calendar.MARCH)+1)+"月的"+c.get(Calendar.DAY_OF_MONTH)+"号");  
    c.add(Calendar.DAY_OF_MONTH,-1);  
    System.out.println("输出的是上个月最后一天");  
    System.out.println((c.get(Calendar.MARCH)+1)+"月的"+c.get(Calendar.DAY_OF_MONTH)+"号");  
    }  
}

The output result is:

The output is the first day of this month

The 1st of March

The output is the last day of this month

The 28th of February

It can be seen that after subtracting one from March 1st, the automatic month automatically changes to February. This is the difference between the roll method and the ad method.

The above is the detailed content of Description of common date classes in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn