開源Java時間工具類Joda-Time體驗

來源:互聯網
上載者:User

標籤:java

import org.joda.time.*;import org.joda.time.format.DateTimeFormat;import org.joda.time.format.DateTimeFormatter;import org.junit.Test;import java.util.Locale;/** * @author by lei zhou on 2017/11/09 14:20. */public class JodaTimeTest {    @Test    public void test() {        // 日期輸出格式        DateTimeFormatter dateTimeFormat = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");        System.out.println("當前日期時間: " + DateTime.now().toString(dateTimeFormat));        System.out.println("當前日期但時間清0: " + DateTime.now().withTimeAtStartOfDay().toString(dateTimeFormat));        System.out.println("本月第一個周日的日期時間: " + getThisMonthFirstSunday().toString(dateTimeFormat));        System.out.println("本周周一的日期時間: " + getThisWeekSunday().toString(dateTimeFormat));        System.out.println("距離元旦天數: " + daysToNewYear(DateTime.now()));        System.out.println("距離元旦月數: " + monthsToNewYear(DateTime.now()));        String[] french = DateTimeUtils.getDateFormatSymbols(Locale.FRANCE).getWeekdays();        String[] japanese = DateTimeUtils.getDateFormatSymbols(Locale.JAPAN).getWeekdays();        String[] korean = DateTimeUtils.getDateFormatSymbols(Locale.KOREA).getWeekdays();        System.out.println("今年每月第一天是周幾: ");        for (int month = 1; month <= 12; month++) {            DateTime monthDateTime = DateTime.now().withTimeAtStartOfDay().withMonthOfYear(month).withDayOfMonth(1);            int index = monthDateTime.dayOfWeek().get() % 7 + 1;            System.out.println(monthDateTime.toString(DateTimeFormat.fullDateTime()) + " 法語:" + french[index] + " 日語:" + japanese[index] + " 韓語:" + korean[index]);        }    }    private int monthsToNewYear(DateTime fromDate) {        DateTime newYear = fromDate.plusYears(1).withDayOfYear(1);        return Months.monthsBetween(fromDate, newYear).getMonths();    }    private int daysToNewYear(DateTime fromDate) {        DateTime newYear = fromDate.plusYears(1).withDayOfYear(1);        return Days.daysBetween(fromDate, newYear).getDays();    }    private DateTime getThisMonthFirstSunday() {        return DateTime.now().withDayOfMonth(1).withDayOfWeek(DateTimeConstants.SUNDAY);    }    private DateTime getThisWeekSunday() {        return DateTime.now().withDayOfWeek(DateTimeConstants.MONDAY);    }}

輸出結果:

當前日期時間: 2017-11-09 16:01:36當前日期但時間清0: 2017-11-09 00:00:00本月第一個周日的日期時間: 2017-11-05 16:01:36本周周一的日期時間: 2017-11-06 16:01:36距離元旦天數: 53距離元旦月數: 1今年每月第一天是周幾: 2017年1月1日 星期日 上午12時00分00秒 CST 法語:dimanche 日語:日曜日 韓語:2017年2月1日 星期三 上午12時00分00秒 CST 法語:mercredi 日語:水曜日 韓語:2017年3月1日 星期三 上午12時00分00秒 CST 法語:mercredi 日語:水曜日 韓語:2017年4月1日 星期六 上午12時00分00秒 CST 法語:samedi 日語:土曜日 韓語:2017年5月1日 星期一 上午12時00分00秒 CST 法語:lundi 日語:月曜日 韓語:2017年6月1日 星期四 上午12時00分00秒 CST 法語:jeudi 日語:木曜日 韓語:2017年7月1日 星期六 上午12時00分00秒 CST 法語:samedi 日語:土曜日 韓語:2017年8月1日 星期二 上午12時00分00秒 CST 法語:mardi 日語:火曜日 韓語:2017年9月1日 星期五 上午12時00分00秒 CST 法語:vendredi 日語:金曜日 韓語:2017年10月1日 星期日 上午12時00分00秒 CST 法語:dimanche 日語:日曜日 韓語:2017年11月1日 星期三 上午12時00分00秒 CST 法語:mercredi 日語:水曜日 韓語:2017年12月1日 星期五 上午12時00分00秒 CST 法語:vendredi 日語:金曜日 韓語:


本文出自 “zl1030的記錄” 部落格,請務必保留此出處http://zl1030.blog.51cto.com/274507/1980326

開源Java時間工具類Joda-Time體驗

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.