• 对日期的各种处理


    public static void main(String[] args) {
    
            System.out.println("************************获取年月日时分秒************************");
            //1、获取年月日时分秒(两种方式)
            Calendar cal = Calendar.getInstance();
            System.out.println("Calendar:" + cal.get(Calendar.YEAR) + "年" +
                            //month:0~11
                    cal.get(Calendar.MONTH) + "月" +
                    cal.get(Calendar.DATE) + "日" +
                    cal.get(Calendar.HOUR_OF_DAY) + "时" +
                    cal.get(Calendar.MINUTE) + "分" +
                    cal.get(Calendar.SECOND) + "秒"
                    );
    
            //Java 8
            LocalDateTime ldt = LocalDateTime.now();
            System.out.println("LocalDateTime:" + ldt.getYear() + "年"
                    //month:1~12
                    + ldt.getMonthValue()+"月"
                    + ldt.getDayOfMonth() + "日"
                    + ldt.getHour()+ "时"
                    + ldt.getMinute()+ "分"
                    + ldt.getSecond()+ "秒"
    
            );
            System.out.println("************************2、获取时间戳************************");
    
            //获取时间戳(三种方式)
            System.out.println(System.currentTimeMillis());
            System.out.println(Calendar.getInstance().getTimeInMillis());
            //java 8
            System.out.println(Clock.systemDefaultZone().millis());
            Instant now = Instant.now();
            System.out.println(now.toEpochMilli());
    
            System.out.println("***********************3、获取某月最后一天************************");
            //获取某月最后一天(2种方式)
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            //获取当前月第一天
            cal.add(Calendar.MONTH,0);
            //设置为1号
            cal.set(Calendar.DAY_OF_MONTH,1);
            String first = format.format(cal.getTime());
            //最后一天
            cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
            String last = format.format(cal.getTime());
            System.out.println("Calendar----first:" + first +"   last:"+last);
    
            //java 8
            LocalDate today = LocalDate.now();
            LocalDate firstDay = LocalDate.of(today.getYear(),today.getMonth(),1);
            LocalDate lastDay = today.with(TemporalAdjusters.lastDayOfMonth());
            System.out.println("LocalDate----first:" + firstDay +"   last:"+lastDay);
    
            System.out.println("***********************4、格式化日期************************");
            //1.Java.text.DataFormat的子类SimpleDateFormat
            //2.java.time.format.DateTimeFormatter
            Date date = new Date();
            System.out.println("SimpleDateFormat----:" + format.format(date));
    
            //java 8
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
            System.out.println("DateTimeFormatter----:" + ldt.format(dtf));
    
            System.out.println("***********************5、获取昨日的当前时刻************************");
            cal.add(Calendar.DATE,-1);
            System.out.println("Calendar----Yesterday:" + format.format(cal.getTime()));
            //java 8
            LocalDateTime ldt2 = ldt.minusDays(1);
            System.out.println("LocalDateTime----Yesterday:" +ldt2.format(dtf));
    
            //计算程序运行时间差
            Instant start = Instant.now();
            //
            Instant end = Instant.now();
            //计算时间差 采用Duration来处理时间戳的差
            Duration timeElapsed = Duration.between(start, end);
            long millis = timeElapsed.toMillis();
            System.out.println("millis = " + millis);
    
        }
  • 相关阅读:
    [转]wireshark 实用过滤表达式(针对ip、协议、端口、长度和内容) 实例介绍
    [转]HTTPS连接的前几毫秒发生了什么
    将生成的数据写入文件
    linux查看网络监听端口
    [转] Linux下移动virtualbox虚拟硬盘丢失eth0
    sockaddr结构体
    以十六进制形式打印字符串
    pta5-9 Huffman Codes (30分)
    pat1068. Find More Coins (30)
    hduoj 2955Robberies
  • 原文地址:https://www.cnblogs.com/fflower/p/12525644.html
Copyright © 2020-2023  润新知