java 时间类 joda_开源Java时间工具类Joda-Time体验

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 日语:金曜日 韩语:

原文:http://zl1030.blog.51cto.com/274507/1980326

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值