org.joda.time時間庫的使用

1.添加maven依賴

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

2.以下列舉了本人平時常用的一些操作(還有很多功能不一一列舉)

    public static void main(String[] args) {
        String time = "2019-11-11 00:00:00";
        String format = "yyyy-MM-dd HH:mm:ss";
        //獲取當前天
        DateTime now = DateTime.now();

        //獲取時間戳
        Long timesTemp = now.getMillis();

        //將時間格式字符串轉爲改時間對象
        DateTime activityTime = DateTime.parse(time, DateTimeFormat.forPattern(format));

        //將時間對象轉爲某種字符串格式
        String formatTime = activityTime.toString(format);

        //往後推天、小時、分鐘等
        DateTime plusDay = now.plusDays(1);
        DateTime plusHours = now.plusHours(2);

        //往前推天、小時、分鐘等
        DateTime minusDay = now.minusDays(1);
        DateTime minusHours = now.minusHours(2);

        //獲取當前天的"0"點
        DateTime nowZeroTime = now.withTimeAtStartOfDay();

        //時間戳轉 DateTime對象
        DateTime newDateTime = new DateTime(1547785369669L);

        //DateTime 轉爲 java.util.Date
        Date date = newDateTime.toDate();

    }

3.java8對時間的處理

https://blog.csdn.net/wsywb111/article/details/79815481

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