Java date and time in class Calendar

A, Calendar class

public abstract class Calendar implements Serializable, Cloneable, Comparable<Calendar>

Calendar class is an abstract class (Calendar class can not create the instance), and it provides some methods to convert between a group such as a calendar field YEAR, MONTH, DAY_OF_MONTH, HOUR other specific instant, and for manipulating the calendar field (obtained e.g. date next week) provides methods; instantly available millisecond value to indicate that it is from the epoch (GMT 00 January 1, 1970 are: 00: 00.000) offset

If you want to create an instance of the Calendar class, subclass object must be constructed through the getInstance method Calendar

 

Second, the common method

1,Calendar.getInstance();

This method returns the Calendar class subclass object; Why say "subclass object returns the Calendar class"? First Calendar class is an abstract class, so-called Calendar instance of the class does not exist, and secondly there are many different time zones on Earth, different time zones will correspond to different subclasses of Calendar class (each country has its own a calendar algorithms, such as the first week of mostly Western countries on Sunday, while China was Monday, and so on)

2,get()

The method accepts YEAR Calendar category, MONTH, DAY_OF_MONTH, HOUR, MINUTE, SECOND calendar field etc.

public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
     calender.set(2019,0,22);
        System.out.println(calendar.get(Calendar.YEAR));
        System.out.println(calendar.get(Calendar.MONTH));//默认从0开始计数,通常+1
        System.out.println(calendar.get(Calendar.DATE));//等同于DAY_OF_MONTH
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
        System.out.println(calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
    }

 

3,getTime()

Conversion to the Date class

public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();

        Date time = calendar.getTime();
        System.out.println(time);
        System.out.println(time.getTime());
        time.setTime(1265871327628L);
        System.out.println(time);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:MM:ss");
        String format = simpleDateFormat.format(time);
        System.out.println(format);
    }

 

 

Three, LocalDateTime class

Java's Date, Calendar type to use is not very convenient, and the Date class (allegedly) has a thread-unsafe and many other disadvantages. If encapsulated simultaneously would be particularly troublesome at each use. So Java8 introduced a thread-safe, simple, highly reliable time package. And the database is also supported LocalDateTime type, at the time when the data storage easier. Java8 The new three relevant time type: LocalDateTime date tenth of a second; LocalDate date; LocalTime time; package of three methods are similar.

public class LocalDateTimeTest {

    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
        int month = now.getMonthValue();
        System.out.println(month);
        LocalDateTime localDateTime = LocalDateTime.of(2008, 12, 31, 22, 21, 22);
        System.out.println(localDateTime);

        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
        System.out.println(zonedDateTime);

        String format = now.format(DateTimeFormatter.ofPattern("YYYY-MM-dd"));
        System.out.println(format);

    }
}

 

Guess you like

Origin www.cnblogs.com/noperx/p/11361212.html