zoukankan      html  css  js  c++  java
  • java8及8之前日期相关类

    java 8日期相关类
    Instant:精确到纳秒的时间戳
    Duration:处理有关基于时间的时间量
    LocalDate:只包含日期,比如:2016-10-20
    LocalTime:只包含时间,比如:23:12:10
    LocalDateTime:包含日期和时间,比如:2016-10-20 23:14:21
    Period:处理有关基于时间的日期数量
    ZoneOffset:时区偏移量,比如:+8:00
    ZonedDateTime:带时区的时间
    Clock:时钟,比如获取目前美国纽约的时间

    日期格式化类:DateTimeFormatter
    示例:

    DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    LocalDateTime localDateTime = LocalDateTime.now();
     System.out.printf("localDateTime:" + localDateTime.format(formatter1) + "
    ");
    
     DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
     LocalDate localDate = LocalDate.now();
     System.out.printf("localDate:" + localDate.format(formatter2) + "
    ");
     System.out.printf("本月第一天:" + localDate.withDayOfMonth(1) + "
    ");
     System.out.printf("本月第一天:" + localDate.with(TemporalAdjusters.firstDayOfMonth()) + "
    ");
    
     Clock clock = Clock.systemDefaultZone();
     Instant instant=clock.instant();
     System.out.printf("Instant:" + instant);
    

    java8 之前日期相关类
    Calendar
    Date
    Timestamp
    例:

    Date date = new Date(); // 获取当前的系统时间
    System.out.println("年份:"+ date.getYear());
            
    Calendar calendar = Calendar.getInstance(); //获取当前的系统时间。
    System.out.println("年:"+ calendar.get(Calendar.YEAR));
    System.out.println("月:"+ (calendar.get(Calendar.MONTH)+1)); System.out.println("日:"+ calendar.get(Calendar.DATE));
    System.out.println("时:"+ calendar.get(Calendar.HOUR_OF_DAY));
    System.out.println("分:"+ calendar.get(Calendar.MINUTE));
    System.out.println("秒:"+ calendar.get(Calendar.SECOND)); 
    

    日期格式化类:SimpleDateFormat
    例:

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    System.out.printf("date:" + format.format(new Date()) + "
    ");
    
    Calendar calendar = Calendar.getInstance();
    System.out.printf("calendar:" + format.format(calendar.getTime()) + "
    ");
    
  • 相关阅读:
    jquery学习笔记1
    javascript常用函数(1):jquery操作select 基本操作
    Angular.js学习笔记
    Mutex, semaphore, spinlock
    Linq 常用方法解释
    装B必备之 快捷键配置
    HttpWebRequest
    HttpClient get post
    js获取url 参数
    整洁代码1
  • 原文地址:https://www.cnblogs.com/lenovo_tiger_love/p/9899094.html
Copyright © 2011-2022 走看看