Development of class time

Key Reference article!

http://www.importnew.com/14857.html

 

 

  • of: static factory method to create the instance of the part
  • from: a static factory method, a similar attempt to extract from the object instance. from () method is not of () method of the type of security
  • now: static factory method, create an instance with the current time
  • parse: static factory method, to give a total string parsing object instance
  • get: Gets the date and time the object of partial state
  • is: Check the description of the target date is correct
  • with: returns a state change part of the date and time copy of the object
  • plus: Returns a time increases, the date and time copy of the object
  • minus: Returns a reduced time, the date and time copy of the object
  • to: the current date and time to convert the objects into the other, the state may be lost portion
  • at: combination with the current time and date objects to another, creating a larger or more complex objects date
  • format: the ability to provide date and time formatting objects

Instant

    public static void main(String[] args) {
        Instant instant= Instant.now();
        //现在时间
        System.out.println(instant);
        //Constant for the 1970-01-01T00:00:00Z epoch instant.
        System.out.println(Instant.EPOCH);
        //This could be used by an application as a "far future" instant.
        System.out.println(Instant.MAX);
        //This is one year earlier than the minimum
        System.out.println(Instant.MIN);
        /**
         * 通过文档获知,正值和负值都是以纪元时间的先后尽心分割
         */
        System.out.println(Instant.ofEpochMilli(86400000));
        System.out.println(Instant.ofEpochSecond(86400));
        //对时间进行矫正,文档中有写
        System.out.println(Instant.ofEpochSecond(86400,100));
        //可以按照始终获取来源进行获取
        System.out.println(Instant.now(Clock.systemDefaultZone()));
        //对不同的日期进行转换,只要是实现了接口的
        System.out.println(Instant.from(ZonedDateTime.now()));
        //获取从纪元时间到现在的秒数
        System.out.println(instant.getEpochSecond());
        //获取纳秒数
        System.out.println(instant.getNano());
        //截取其中的一部分
        System.out.println(instant.get(ChronoField.NANO_OF_SECOND));
        //比较时间
        System.out.println(instant.compareTo(Instant.EPOCH));
        //根据分和秒进行纠正
        System.out.println(instant.adjustInto(Instant.now()));
        //形成偏移日期
        System.out.println("atOffset:"+instant.atOffset(ZoneOffset.ofHours(2)));
        //设置时区
        System.out.println(instant.atZone(ZoneId.systemDefault()));
        //截取其中的一部分
        System.out.println(instant.getLong(ChronoField.NANO_OF_SECOND));
        System.out.println(instant.isAfter(instant));
        System.out.println(instant.isBefore(instant));
        System.out.println(instant.isSupported(ChronoField.NANO_OF_SECOND));
        //时间日期相加减
        System.out.println(instant.minus(Duration.ofDays(100000)));
        System.out.println(instant.plusMillis(1000000));
        //显示日期时间段范围
        System.out.println(instant.range(ChronoField.NANO_OF_SECOND));
        //截断日期到一个值
        System.out.println(instant.truncatedTo(ChronoUnit.DAYS));
        //此时间与结束瞬间的时间量
        System.out.println(instant.until(Instant.now(),ChronoUnit.SECONDS));

LocalDate:

import java.time.*;
import java.time.chrono.IsoEra;
import java.time.chrono.MinguoDate;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalQueries;

public class test2 {
    public static void main(String[] args) {
        LocalDate localDate = LocalDate.now();
        System.out.println(localDate);
        System.out.println(localDate.getClass());
        System.out.println(LocalDate.MAX);
        System.out.println(LocalDate.now());
        System.out.println(LocalDate.MIN);
        //获得特定时区的localDate
        System.out.println(LocalDate.now(ZoneOffset.UTC));
        //获取特定的时钟
        System.out.println(LocalDate.now(ZoneId.systemDefault()));
        //使用不同的历法
        System.out.println(LocalDate.from(MinguoDate.now()));
        //距离纪元时间的偏移量的值
        System.out.println(LocalDate.ofEpochDay(10000));
        System.out.println(LocalDate.of(2001, 01, 02));
        System.out.println(LocalDate.ofYearDay(2011, 100));
        //对CharSequence进行解析抓换
        System.out.println(LocalDate.parse("2019-03-05"));
        //对现有的时间类的精度以及属性信息进行查询,可以获得很多信息
        System.out.println(localDate.query(TemporalQueries.precision()));
        //获取纪元信息等
        System.out.println(localDate.get(ChronoField.ERA));
        //调整至相同类型获取时瞬,这里要求是同一类型
        System.out.println(localDate.adjustInto(LocalDate.now()));
        //1970-1-1距现在的时间
        System.out.println(localDate.getLong(ChronoField.EPOCH_DAY));
        //得到一个period对象,用以返回相对应的对象
        System.out.println(localDate.until(LocalDate.now()).getClass());
        //返回一个部分状态改变的对象
        System.out.println(localDate.with(IsoEra.BCE));
        //创建一个localDateTime
        System.out.println(localDate.atStartOfDay());
    }
}

localdateti to

public class test3 {
    public static void main(String[] args) {
        System.out.println(LocalDateTime.MAX);
        System.out.println(LocalDateTime.MIN);
        System.out.println(LocalDateTime.now());
        LocalDateTime localDateTime=LocalDateTime.now();
        //从相似方法中获取方法
        System.out.println(LocalDateTime.from(LocalDateTime.MAX));
        System.out.println(localDateTime);
        //本地时间加偏移量 OffsetDateTime转化为这样的日期类型
        System.out.println(localDateTime.atOffset(ZoneOffset.ofHours(18)).getClass());
        //截短了的日期
        System.out.println(localDateTime.truncatedTo(ChronoUnit.DAYS));
        //获取时间区间的有效值
        System.out.println(localDateTime.range(ChronoField.EPOCH_DAY));
        //获取现在使用的时间系统值
        System.out.println(localDateTime.getChronology());
    }
}

LocalTime

import java.time.*;
import java.util.Date;

public class test4 {
    public static void main(String[] args) {
        System.out.println(LocalTime.now());
        LocalTime localTime=LocalTime.now();
        //从相似的类型中提取
        System.out.println(LocalTime.from(LocalDateTime.now()));
        //最大最小值
        System.out.println(LocalTime.MIN);
        System.out.println(LocalTime.MAX);
        System.out.println(LocalTime.MIDNIGHT);
        //指定时区来源
        System.out.println(LocalTime.now(Clock.systemDefaultZone()));
        //instant到localDateTime的指定转换方式
        System.out.println(LocalTime.now(ZoneId.systemDefault()));
        System.out.println(LocalTime.of(10,20));
        //获取十二点
        System.out.println(LocalTime.NOON);
        //转变时间为offset
        System.out.println(localTime.atOffset(ZoneOffset.ofHours(1)));
        //给其添加时间
        System.out.println(localTime.atDate(LocalDate.now()));
        //现在所处的纳秒
        System.out.println(localTime.toNanoOfDay());
    }
}

DatetimeFormatter

public class test5 {
    public static void main(String[] args) {
        //可以理解为线程安全的simpleDateFormat
        DateTimeFormatter dtf=DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
        System.out.println(dtf);
        System.out.println(dtf.format(LocalDateTime.now()));
        dtf=DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG);
        System.out.println(dtf.format(LocalDateTime.now()));
        dtf=DateTimeFormatter.ofPattern("yyyy/MM/dd");
        System.out.println(dtf.parse("2019/05/01").getClass());
        System.out.println(dtf.parse("2010/01/01").get(ChronoField.YEAR_OF_ERA));
        System.out.println(dtf.getDecimalStyle());
        System.out.println(dtf.getLocale());
        //获取了那些对象
        System.out.println(dtf.getResolverFields());
    }
}

Duration

import java.time.Duration;
import java.time.LocalDateTime;

public class test6 {
    public static void main(String[] args) {
        Duration duration=Duration.ofDays(100);
        System.out.println(duration.getUnits());
        duration=duration.abs();
        System.out.println(duration);
        duration=Duration.from(Duration.ZERO);
        System.out.println(duration.getClass());
        duration=Duration.ofDays(20);
        System.out.println(duration.dividedBy(100));
        //两者的差值,这里需要使用最小单位是秒
        System.out.println(Duration.between(LocalDateTime.now(),LocalDateTime.MIN));
    }
}

Period

public class test7 {
    public static void main(String[] args) {
        Period period=Period.of(1,1,1);
        System.out.println(period);
        Period period1=Period.ZERO;
        System.out.println(period1);
        Period period2=Period.from(period1);
        System.out.println(period2);
        //为了方便相加减
        System.out.println(period.negated());
    }
}

ChronoUnit

import java.time.temporal.ChronoUnit;

public class test8 {
    public static void main(String[] args) {
        System.out.println(ChronoUnit.values());
        ChronoUnit chronoUnit=ChronoUnit.DAYS;
        System.out.println(chronoUnit.isDateBased());
        for(ChronoUnit c:ChronoUnit.values()){
            System.out.println(c+" "+c.getDuration());
        }
        System.out.println(chronoUnit.isDurationEstimated());
    }
}

 

Guess you like

Origin blog.csdn.net/qq_29164145/article/details/90484745