JAVA8时间类

package com.example.demo;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.TemporalField;
import java.time.temporal.WeekFields;

public class Demo {

    public static void main(String[] args) {

        /**
         * Java8新增的所有日期类都在包java.time下,而且都是final class和线程安全。
         * PS:Java8以前的日期类Date、Calendar、SimpleDateFormat等都存在线程安全问题。
         *
         * LocalDateTime:日期+时间 yyyy-MM-dd HH:mm:ss,替代Calendar
         * LocalDate:日期,yyyy-MM-dd
         * LocalTime:时间,HH:mm:ss
         *
         * YearMonth:年月,比如信用卡的日期类型
         * MonthDay:月日,比如生日
         * DateTimeFormatter:日期格式化类,替代SimpleDateFormat
         * Instant:替代Date
         * java8中提供三个类(LocalDate,LocalTime,LocalDateTime),
         * 它们分别对年月日、时分秒和年月日时分秒进行单独的处理,
         * 使用时可以根据自己的实际情况进行选择,它们的API类似
         */
       /* LocalDateTime localDateTime=LocalDateTime.now();
        System.out.println(localDateTime);//2020-09-28T14:51:14.231

        LocalTime localTime=LocalTime.now();
        System.out.println(localTime);//14:51:14.232

        LocalDate localDate=LocalDate.now();
        System.out.println(localDate);//2020-09-28*/

        //还有很多枚举类,月份枚举Month、星期枚举DayOfWeek、Year类(可以判断闰年)等等。

        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        //todo LocalDateTime类 ,,,LocalDateTime:日期+时间 yyyy-MM-dd HH:mm:ss,替代Calendar
//        LocalDateTime localDateTime2 = LocalDateTime.parse("2018-01-01 17:30:30", pattern);
//        System.out.println("当天是周几: " + localDateTime2.getDayOfWeek());
//        System.out.println("ISO_LOCAL_DATE_TIME: " + localDateTime2.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
//        System.out.println("判断日期大小: " + localDateTime2.isBefore(LocalDateTime.now()));
//        System.out.println("判断日期大小: " + localDateTime2.isEqual(LocalDateTime.now()));
        //todo LocalDate类  ,,,,,,,,,LocalDate:日期,yyyy-MM-dd
//        LocalDate localDate = LocalDate.parse("2018-06-07 17:30:30", pattern);
  /*      LocalDate localDate = LocalDate.now();
        System.out.println("localDate: " + localDate);
        System.out.println("ISO_LOCAL_DATE: " + localDate.format(DateTimeFormatter.ISO_LOCAL_DATE));
        //当月第一天:2020-09-01
        System.out.println("当月第一天:" + LocalDate.now().withDayOfMonth(1));
        //当前年2月最后一天: 2020-02-29
        System.out.println("当前年2月最后一天: " + LocalDate.now().withMonth(2).with(TemporalAdjusters.lastDayOfMonth()));
        //下周的日期两个方式,日、月、年类似
        //下周开始日期: 2020-10-05
        System.out.println("下周日期: " + localDate.plus(1, ChronoUnit.WEEKS));
        //下周开始日期: 2020-10-05
        System.out.println("下周日期: " + localDate.plusWeeks(1));
        //下下周开始日期: 2020-10-12
        System.out.println("下下周日期: " + localDate.plusWeeks(2));
        //2020年10月第一个星期天: 2020-10-04
        System.out.println("2020年10月第一个星期天: " + LocalDate.of(2020, 10, 1)
                .with(TemporalAdjusters.firstInMonth(DayOfWeek.SUNDAY)));*/

        //TODO LocalTime类
        //01:01:01
//        System.out.println("LocalTime: " + LocalTime.of(1, 1, 1));

        //TODO **之差只要改变后面的枚举类型就行
     /*   LocalDate startDate = LocalDate.parse("2020-09-01");
        LocalDate endDate = LocalDate.parse("2020-09-30");
        //周数差: 4
        System.out.println("周数差: " + startDate.until(endDate, ChronoUnit.WEEKS));
        //天数差: 29
        System.out.println("天数差: " + startDate.until(endDate, ChronoUnit.DAYS));
*/

        //TODO 本年第几周
        //WeekFields.ISO代表每周从周一开始算。
        //WeekFields.SUNDAY_START代表每周从周日开始算。
        TemporalField weekFields = WeekFields.ISO.weekOfMonth();
//        TemporalField weekFields = WeekFields.SUNDAY_START.weekOfMonth();

        //注意
        //if the 1st day of the year is a Monday, week one starts on the 1st and there is no week zero
        //if the 2nd day of the year is a Monday, week one starts on the 2nd and the 1st is in week zero
        //if the 4th day of the year is a Monday, week one starts on the 4th and the 1st to 3rd is in week zero
        //if the 5th day of the year is a Monday, week two starts on the 5th and the 1st to 4th is in week one
        //就是1月第一周天数<=3(周一是星期起始天),那么这些天都是第0周。

        //第几周:5
//        System.out.println("第几周:" + LocalDate.of(2020, 9, 28).get(weekFields));

        //TODO 指定时间之后或之前XXX的时间
/*        LocalDateTime now = LocalDateTime.now();
        System.out.println("当前时间" + now);
        LocalDateTime localDateTime = now.minusDays(2);
        //当前时间之前2天:2020-09-26 15:34:48
        //当前时间之前2天:2020-09-26
        //当前时间之前2天:15:35:33
        System.out.println("当前时间之前2天:" + localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        System.out.println("当前时间之后2天:" + now.plusDays(2));
        System.out.println("当前时间之前2周:" + now.minusWeeks(2));
        System.out.println("当前时间之后2周:" + now.plusWeeks(2));
        System.out.println("当前时间之前2月:" + now.minusMonths(2));
        System.out.println("当前时间之后2月:" + now.plusMonths(2));
        System.out.println("当前时间之前2年:" + now.minusYears(2));
        System.out.println("当前时间之后2年:" + now.plusYears(2));
        System.out.println("当前时间之前2小时:" + now.minusHours(2));
        System.out.println("当前时间之后2小时:" + now.plusHours(2));
        System.out.println("当前时间之前2分钟:" + now.minusMinutes(2));
        System.out.println("当前时间之后2分钟:" + now.plusMinutes(2));
        System.out.println("当前时间之前2秒:" + now.minusSeconds(2));
        System.out.println("当前时间之后2秒:" + now.plusSeconds(2));*/

        //TODO 比较时间先后
    /*    LocalDateTime now = LocalDateTime.now();
        System.out.println("当前时间:"+now);
        LocalDateTime appointTime = LocalDateTime.of(2019, 1, 17, 9, 32, 12);
        System.out.println("指定时间:"+appointTime);
        System.out.println(now.isAfter(appointTime));
        System.out.println(now.isBefore(appointTime));*/


        //TODO 格式化时间格式及按照一定格式解析
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("HH:mm:ss");
        //格式化
        String format = now.format(formatter);
        String format2 = now.format(formatter2);
        String format3 = now.format(formatter3);
        System.out.println("格式化1:" + format);
        System.out.println("格式化2:" + format2);
        System.out.println("格式化3:" + format3);
        //解析
        LocalDateTime localDateTime = LocalDateTime.parse(format, formatter);
        LocalDate localDate = LocalDate.parse(format2, formatter2);
        LocalTime localTime = LocalTime.parse(format3, formatter3);
        System.out.println("解析1:" + localDateTime);
        System.out.println("解析2:" + localDate);
        System.out.println("解析3:" + localTime);

    }
}

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271