美文网首页我爱编程
六号线晚报0527

六号线晚报0527

作者: z0nk1n | 来源:发表于2018-05-27 23:23 被阅读0次

    天气:阴 风力:微风

    Java时间处理
    Java8 time包

    public class TimeUtil {
    
        public static void main(String[] args) {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
            long currentTImeMillis = System.currentTimeMillis();
            System.out.println(currentTImeMillis); //当地时间戳  1527433077277
            System.out.println(df.format(new Date(currentTImeMillis)));//2018-05-27 22:57:57:277
            System.out.println(df.format(new Date(currentTImeMillis / 1000 * 1000)));//2018-05-27 22:57:57:000
            System.out.println(System.nanoTime()); //虚拟机时间有误差一般用于计算时间间隔
    
            System.out.println(Instant.now());//2018-05-27T14:57:57.281Z
            System.out.println(LocalDateTime.now());//2018-05-27T22:57:57.386
            System.out.println(LocalDate.now());//2018-05-27
            String localTime = String.valueOf(LocalTime.now());
            System.out.println(localTime.substring(0, localTime.length() - 4));//22:57:57
            System.out.println(LocalTime.now());//22:57:57.386
            System.out.println(LocalTime.now().getHour());//22
            System.out.println(LocalTime.now().getMinute());//57
            System.out.println(LocalTime.now().getSecond());//57
    
            LocalTime localTime1 = LocalTime.of(23, 12, 12);
            System.out.println(localTime1.plusHours(2));//01:12:12
    
            LocalDate localDate = LocalDate.of(2018, 5, 27);
            System.out.println(localDate.plusDays(10));//2018-06-06
            System.out.println(LocalDate.now().equals(localDate));//true
    
            LocalDate localDate1 = LocalDate.of(2018, 6, 28);
            System.out.println(LocalDate.now().compareTo(localDate1));//-1
            System.out.println(localDate1.plus(1, ChronoUnit.WEEKS));//2018-07-05
            System.out.println(localDate1.plus(1, ChronoUnit.DAYS));//2018-06-29
        }
    }
    
    
    • 未完待续

    相关文章

      网友评论

        本文标题:六号线晚报0527

        本文链接:https://www.haomeiwen.com/subject/rtoijftx.html