java objects Time Date, Calendar and LocalDate / LocalDateTime

 I. Introduction

  Date: java.util.Date package, including dates, times, number of milliseconds.

  Calendar: java.util.Calendar package, abstract modification, many ways Date of outdated moved to the Calendar class.

  LocalDate / LocalDateTime: java.time.LocalDate / java.time.LocalDateTime package, this class is immutable and thread-safe. Using the equalscomparative method.

二, java.util.Date

  Date of setXXX () and getXXX () Gets Date, methods Day, Year, Hours, TimezoneOffSet the like have been abandoned.

 

三, java.util.Calendar

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

  The Calendarclass is an abstract class, the method may be a conversion between a particular moment and a set of calendar fields such as YEAR, , MONTH, DAY_OF_MONTH, HOURand the like, and means for manipulating the calendar fields, such as date of acquisition of next week. Instant in time can be expressed in milliseconds value, which is dated 1970. 1. 1 Day 00:00 00: 00.000 GMT (Gregorian) of Epoch offset. The class also provides other methods for achieving specific fields and calendar systems external to the package.

 

      Calendar c=Calendar.getInstance();
        c.add(Calendar.DATE,-1);//减一天
        StringBuffer bf=new StringBuffer();
        bf.append(c.get(Calendar.YEAR)+"年");
        bf.append(c.get(Calendar.MONTH)+1+"月");
        bf.append(c.get(Calendar.DATE)+"日");
        bf.append(c.get(Calendar.HOUR_OF_DAY)+"时");//24小时制
        //bf.append(c.get(Calendar.HOUR)+"时");//12小时制
        bf.append(c.get(Calendar.MINUTE)+"分");
        bf.append(c.get(Calendar.SECOND)+"秒");
        bf.append("本年第"+c.get(Calendar.DAY_OF_YEAR)+"天;");
        bf.append("本月第"+c.get(Calendar.DAY_OF_WEEK_IN_MONTH)+"周;");
        bf.append("本周第"+c.get(Calendar.DAY_OF_WEEK)+"天;");
        bf.append(c.get(Calendar.WEEK_OF_MONTH)+";今年第"+c.get(Calendar.WEEK_OF_YEAR)+"周");    

 

四、java.time.LocalDate/java.time.LocalDateTime

  public final class extends Object implements Temporal, TemporalAdjuster, ChronoLocalDate, Serializable

  LocalDate / LocalDateTime a date-time object is immutable, date, and is generally regarded as the date. You can also access other date fields, such as date, day of week and day of week. For example, the value "2 October 2007" can be stored LocalDate.

This class does not store or indicate the time or time zone. Rather, it is the date of the description, for a birthday. It can not represent instant messaging timeline, without additional information, such as offset, or time zone.

ISO-8601日历系统是当今世界绝大多数的现代民用日历系统. It is the equivalent of today's leap year rule has been applied to the Gregorian calendar system of everyday life. For most applications written today, ISO-8601 rules is entirely appropriate. However, any use of historical dates and asked them to correct the application will find ISO-8601 method inappropriate.

 

/ ** 
 * Get the current time, specify a particular time. Subtraction of the time 
 * / 
the LocalDateTime LDT = LocalDateTime.now (); LocalDate.now (); 
LocalTime.now (); 
ldt.plusHours (8L);
ldt.plusSeconds (10L); ldt.format (DateTimeFormatter.ofPattern (
"the mM-dd-YYYY HH: mm: SS" )); the LocalDateTime LDT2 = ldt.minus (. 5 , ChronoUnit.MONTHS); ldt2.atZone (ZoneId.systemDefault ()); LDT2 = ldt2.withHour (2 ); LDT2 ldt2.withYear = (, 2015 ); LDT2 = ldt2.with (ChronoField.MONTH_OF_YEAR,. 3 );
/ **
* calculated interval * for the Duration day, hour, minute, second, etc. calculation * Period used for Year, Month calculation * / Duration duration
= Duration.between(ldt,ldt2); duration.toDays(); duration.toHours(); duration.toMinutes(); Period period = Period.between(ldt.toLocalDate(),ldt2.toLocalDate()); period.getYears(); period.getMonths(); period.toTotalMonths();
/**
*与Date的转化,format为String
*/ Date date
= Date.from(ldt2.atZone(ZoneId.systemDefault()).toInstant()); LocalDateTime dateTime = LocalDateTime.ofInstant(date.toInstant(),ZoneId.systemDefault()); ldt3.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
/**
* Requires made according to the needs of the day zero * / The LocalDateTime today_start
= LocalDateTime.of (LocalDate.now (), LocalTime.MIN); // day zero String td_st_str = today_start.format (DateTimeFormatter.ofPattern ( "yyyyMMddHHmmss" )); // get the end of the time of day the LocalDateTime today_end = LocalDateTime.of (LocalDate.now (), LocalTime.MAX); // day zero / **
* date String mutual conversion with the LocalDateTime
* / the DateTimeFormatter DF
= DateTimeFormatter.ofPattern ( "the mM-dd-YYYY HH: mm: SS" ); the LocalDateTime Time = LocalDateTime.now (); String localTime, = df.format (Time); the LocalDateTime LDTLocalDateTime.parse = ( "2016-08-08 18:08:08" , DF); System.out.println ( "turn into the LocalDateTime time of type String:" + localTime,); System.out.println ( "Type String time to turn into the LocalDateTime: "+ LDT); / ** * Get milliseconds long by LocalDateTime type, attention, Eastern eight districts * / Long newSecond1
= LocalDateTime.now () toInstant (ZoneOffset.of (." +. 8 " .)) toEpochMilli ();

 

Guess you like

Origin www.cnblogs.com/choua1997/p/11570232.html