joda-Time,JAVA8時間API 示例解析

JodaTime

joda:網址

在這裏插入圖片描述

maven座標

<dependency>
  <groupId>joda-time</groupId>
  <artifactId>joda-time</artifactId>
  <version>2.10.6</version>
</dependency>

示例 :方法使用

public class JodaTimeTest1 {
    public static void main(String[] args) {
        DateTime today = new DateTime();
        DateTime time = today.plusDays(1);//獲取明天
        System.out.println(today.toString("yyyy-MM-dd"));
        System.out.println(time.toString("yyyy-MM-dd"));

        System.out.println("===========");
        DateTime dateTime = today.withDayOfMonth(1);//獲取本月第一天
        System.out.println(dateTime.toString("yyyy-MM-dd"));

        System.out.println("===========");

        LocalDate localDate = new LocalDate();
        System.out.println(localDate); //當前時區

        System.out.println("===========");

        /*
        計算當前日期後一個月的第一天的日期
        localDate: 當前日期
        plusMonths(3) : 加三個月
        dayOfMonth():獲取月份的屬性
        withMinimumValue(): 月份最小那天日期
        withMaximumValue(): 月份最大日期
         */
        LocalDate date = localDate.plusMonths(3).dayOfMonth().withMinimumValue();
        System.out.println(date);

        /*
            計算兩年前第三個月最後一天的日期
            minusYears(2) 獲取2年前屬性
            monthOfYear() 獲取年月份屬性
            setCopy(3) 拿到第三個月份
            dayOfMonth() 獲取月份天屬性
            withMaximumValue() 獲取最後一天日期
         */
        DateTime dateTime1 = new DateTime();
        DateTime dt = dateTime1.minusYears(2).monthOfYear().setCopy(3).dayOfMonth().withMaximumValue();
        System.out.println(dt.toString("yyyy-MM-dd"));
    }
}

示例 :使用jodaTime轉換時間類型

public class JodaTimeTest2 {

    /**
     * 將標準的utc轉換成Date類型
     * 標準的TUC時間:2014-11-04T09:22:54.876Z
     */

    public static Date converrUTC2Date(String utcDate) {
        try {
            DateTime dateTime = DateTime.parse(utcDate, DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
            return dateTime.toDate();
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 將Date類型轉換成標準的utc
     * 標準的TUC時間:2014-11-04T09:22:54.876Z
     */
    public static String convertDate2UTC(Date javaDate) {
        DateTime dateTime = new DateTime(javaDate, DateTimeZone.UTC);
        return dateTime.toString();
    }

    /**
     * 將Date時間轉換成指定格式
     *
     * @param javaDate
     * @param dataFormat
     * @return
     */
    public static String convertDate2LocalByDateFormat(Date javaDate, String dataFormat) {
        DateTime dateTime = new DateTime(javaDate);
        return dateTime.toString(dataFormat);
    }

    public static void main(String[] args) {
        System.out.println(JodaTimeTest2.converrUTC2Date("2014-11-04T09:22:54.876Z"));

        System.out.println(JodaTimeTest2.convertDate2UTC(new Date()));

        System.out.println(JodaTimeTest2.convertDate2LocalByDateFormat(new Date(), "yyyy-MM-dd HH:mm:ss"));


    }
}

Java8 時間新特性API

示例:日常方法使用

/**
 * LocalDate 關注的是日期 yyyy-MM-dd
 * * LocalTime 關注的是時分秒 20:59
 * Clock 時鐘 表示當前時刻
 * ZoneId 表示時區
 */
public class Java8TimeTest {
    public static void main(String[] args) {
        LocalDate localDate = LocalDate.now();
        System.out.println("當前時間= " + localDate);//當前時間= 2020-06-16
        System.out.println(localDate.getYear() + " ," + localDate.getMonthValue() + " ," + localDate.getDayOfMonth());//2020 ,6 ,16

        System.out.println("------------");
        //LocalDate一個日期沒有時區
        LocalDate localDate1 = LocalDate.of(2020, 6, 18);//構建日期
        System.out.println(localDate1);//2020-06-18

        System.out.println("------------");

        LocalDate localDate2 = LocalDate.of(2021, 3, 28);
        //構建一個月日 日期
        MonthDay monthDay = MonthDay.of(localDate2.getMonth(), localDate2.getDayOfMonth());
        MonthDay monthDay2 = MonthDay.from(LocalDate.of(2021, 3, 28));//from 來自

        /*
        monthDay.equals 已經被重新判斷月日是否爲同一天
         */
        System.out.println(monthDay.equals(monthDay2));//true

        System.out.println("------------");
        //LocalTime 關注的是時分秒
        LocalTime localTime = LocalTime.now();
        System.out.println(localTime);//21:17:12.890
        /*
            當前時間加上2小時在加上20分鐘
         */
        LocalTime time = LocalTime.now().plusHours(2).plusMinutes(20);
        System.out.println(time);

        System.out.println("------------");

        /*
        表示添加的2是什麼單位
            對指定的月 周 天 加上2

            例 對當前的日期加上兩週
         */
        LocalDate localDate3 = localDate.plus(2, ChronoUnit.WEEKS);
        System.out.println(localDate3);//2020-07-01

        System.out.println("------------");

        //當前時間減掉兩個月
        LocalDate localDate4 = localDate.minus(2, ChronoUnit.MONTHS);
        System.out.println(localDate4);//2020-04-17

        System.out.println("------------");
        //獲取電腦當前默認的時區
        Clock clock = Clock.systemDefaultZone();
        System.out.println(clock);//SystemClock[Asia/Shanghai]
        System.out.println(clock.millis());//1592375414691 當前毫秒

        System.out.println("--------------");

        LocalDate localDate5 = LocalDate.now();
        LocalDate localDate6 = LocalDate.of(2020, 6, 17);
        //判斷localDate5是不是在localDate6之前
        System.out.println(localDate5.isBefore(localDate6));//false
        //判斷localDate5是不是在localDate6之後
        System.out.println(localDate5.isAfter(localDate6));//false
        //判斷localDate5是不是等於指定時間localDate6
        System.out.println(localDate5.isEqual(localDate6));//true


        System.out.println("-------------------------");

        //獲取所有時區
        Set<String> ids = ZoneId.getAvailableZoneIds();
//        ids.forEach(System.out::println);
        Set<String> stringSet = new TreeSet<String>();
        stringSet.addAll(ids);//排序
        stringSet.forEach(System.out::println);

        System.out.println("---------------------");

        ZoneId zoneId = ZoneId.of("Asia/Shanghai");
        // new 一個帶有日期時間的對象
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);//2020-06-17T20:13:21.235

        //生成一個帶有時區的對象
        ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, zoneId);
        System.out.println(zonedDateTime);//2020-06-17T20:13:21.235+08:00[Asia/Shanghai]

        System.out.println("---------------------");
        //帶有年月的時間對象
        YearMonth yearMonth = YearMonth.now();
        System.out.println(yearMonth.lengthOfMonth());//當前月份有多少天 30
        System.out.println(yearMonth.isLeapYear());//是否是閏年

        System.out.println("--------------");

        YearMonth yearMonth1 = YearMonth.of(2020, 6);
        System.out.println(yearMonth1);//2020-06
        System.out.println(yearMonth.lengthOfYear());//今年有多少天  366
        System.out.println(yearMonth.lengthOfMonth());//這個月有多少天 30

        System.out.println("----------------");

        LocalDate localDate7 = LocalDate.now();
        LocalDate localDate8 = LocalDate.of(2040, 3, 18);

        /*
            獲取兩個日期之間的間隔
            2020-6-18 2040-3-18
            間隔19年9個月1天
            getDays getMonths getYears之間個間隔對應的年月日
            不會去將年轉換成月 月轉成天

         */
        Period period = Period.between(localDate8, localDate7);
        System.out.println(period.getDays());//30 間隔天數
        System.out.println(period.getMonths());//2 間隔月份
        System.out.println(period.getYears());//間隔年

        System.out.println("----------------");
        //獲取當前時刻
        System.out.println(Instant.now());//2020-06-17T12:48:51.030Z

    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章