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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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()));
         System.out.println( "当前月份最后一天日期: "  +
                 DateTime.now().withDayOfMonth( 1 )
                         .monthOfYear().addToCopy( 1 )
                         .dayOfMonth().addToCopy(- 1 )
                         .withTimeAtStartOfDay()
                         .toString(dateTimeFormat));
 
         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]);
         }
 
         DateTime birthday = DateTime.parse( "1981-10-30 8:00:00" , dateTimeFormat);
         System.out.println( "距离出生已过去多少年: "  + Years.yearsBetween(birthday, DateTime.now()).getYears());
         System.out.println( "距离出生已过去多少天: "  + Days.daysBetween(birthday, DateTime.now()).getDays());
         System.out.println( "距离出生已过去多少分钟: "  + Minutes.minutesBetween(birthday, DateTime.now()).getMinutes());
     }
 
     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);
     }
}

输出结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
当前日期时间: 2017-11-09 16:47:37
当前日期但时间清0: 2017-11-09 00:00:00
本月第一个周日的日期时间: 2017-11-05 16:47:37
本周周一的日期时间: 2017-11-06 16:47:37
距离元旦天数: 53
距离元旦月数: 1
当前月份最后一天日期: 2017-11-30 00:00:00
今年每月第一天是周几: 
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 日语:金曜日 韩语:
距离出生已过去多少年: 36
距离出生已过去多少天: 13159
距离出生已过去多少分钟: 18949487

本文转自   zl1030   51CTO博客,原文链接:http://blog.51cto.com/zl1030/1980326
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.hexiang.utils; import java.text.SimpleDateFormat; import java.util.*; public class CalendarUtil { public static void main(String args[]) { System.out.println("First day of week is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getFirstDateByWeek(new Date()))); System.out.println("Last day of week is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getLastDateByWeek(new Date()))); System.out.println("First day of month is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getFirstDateByMonth(new Date()))); System.out.println("Last day of month is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getLastDateByMonth(new Date()))); } /** * 获得所在星期的第一天 */ public static Date getFirstDateByWeek(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); int today = now.get(Calendar.DAY_OF_WEEK); int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一 now.set(Calendar.DATE, first_day_of_week); return now.getTime(); } /** * 获得所在星期的最后一天 */ public static Date getLastDateByWeek(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); int today = now.get(Calendar.DAY_OF_WEEK); int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一 int last_day_of_week = first_day_of_week + 6; // 星期日 now.set(Calendar.DATE, last_day_of_week); return now.getTime(); } /** * 获得所在月份的最后一天 * @param 当前月份所在的时间 * @return 月份的最后一天 */ public static Date getLastDateByMonth(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); now.set(Calendar.MONTH, now.get(Calendar.MONTH) + 1); now.set(Calendar.DATE, 1); now.set(Calendar.DATE, now.get(Calendar.DATE) - 1); now.set(Calendar.HOUR, 11); now.set(Calendar.MINUTE, 59); now.set(Calendar.SECOND, 59); return now.getTime(); } /** * 获得所在月份的第一天 * @param 当前月份所在的时间 * @return 月份的第一天 */ public static Date getFirstDateByMonth(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); now.set(Calendar.DATE, 0); now.set(Calendar.HOUR, 12); now.set(Calendar.MINUTE, 0); now.set(Calendar.SECOND, 0); return now.getTime(); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值