Joda Time使用小结
一、Joda Time基础操作
1、 构造指定时间
// 明确给出年月日时分秒,同时还可以指定毫秒
DateTime dateTime = new DateTime(2017,9,14,20,30,0);
// 使用时间戳构造
Datetime dateTime = new DateTime(1505371053358L);
// 使用字符串构造,使用字符串构造需要自己定义pattern
String date = "2017-09-14 20:30:00";
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
DateTime dateTime = dateTimeFormatter.parseDateTime(date);
// 指定时区构造时间
DateTime dateTime = new DateTime(DateTimeZone.forTimeZone(TimeZone.getTimeZone("Asia/Shanghai")));
注意:”Asia/Shanghai”是国际时区Id,该ID可以通过JDK代码获取,代码如下:
String[] zones = TimeZone.getAvailableIDs();
for (String zone : zones) {
System.out.println(zone);
}
2、获取当前时间的时间戳
// JDK
long currentTimeOfMills = System.currentTimeMillis();
// Joda Time
long currentTimeOfMills = DateTime.now().getMillis();
3、获得当前时间的时区
DateTimeZone zone = DateTime.now().getZone();
4、 获取指定时区的当前时间
DateTimeZone gmt = DateTimeZone.forID("GMT");
DateTime dateTime = DateTime.now().toDateTime(gmt);
二、Joda Time 对年月日的一些简单操作。
1、 获取月初第一天和月末最后一天
DateTime dateTime = new DateTime();
// 月初第一天
DateTime theFirstDateOfMonth = dateTime.dayOfMonth().withMinimumValue();
// 当前月最后一天
DataTime theEndDataOfMonth = dateTime.dayOfMonth().withMaximumValue();
// 这一天是几号
int day = dateTime.getDayOfMonth();
// 这一天是哪月
int month = dateTime.getMothOfYear();
// 这一天是哪年
int year = dateTime.getYear();
// 判断本月是不是9月
if(dateTime.getDayOfMonth() == DateTimeConstants.SEPTEMBER){
//TODO
}
// 获取相对于当前时间的月份,比如获取上个月的时间或者下个月的是时间,方法minusMoths接受一个int的参数,如果这个参数等于0,代表本月,大于0代表已经过去的时间,小于0代表还没有到来的时间
LocalDate lastDayOfMonth = new LocalDate().minusMonths(1).dayOfMonth().withMaximumValue();
2、关于星期的操作
DateTime dateTime = new DateTime();
// 今天是星期几
int week = dateTime.getDayOfWeek();
// 判断今天是不是星期三
if(dateTime.getDayOfWeek() == DateTimeConstants.WEDNESDAY){
// TODO
}
注意:DateTimeConstants中包含了许多你需要的常量,而不用你自己去定义,比如星期、月份、上午还是下午都有哦
3、计算时间差
注意开始时间与结束时间参数位置,如果开始时间小于结束时间,得到的天数是正数,否则就是负数哦!
DateTime currentDateTime = new DateTime();
DateTime targetDateTime = new DateTime(2017,10,1,0,0,0);
// 相差多少年
int years = Years.yearsBetween(currentDateTime,targetDateTime).getYears();
// 相差多少月
int months = Months.monthsBetween(currentDateTime,targetDateTime).getMonths();
// 距离国庆放假还有多少天,嘎嘎!
int days = Days.daysBetween(currentDateTime,targetDateTime).getDays();
// 相差多少小时
int hours = Hours.hoursBetween(currentDateTime,targetDateTime).getHours();
// 相差多少分钟
int minutes = Minutes.minutesBetween(currentDateTime,targetDateTime).getMinutes();
// 相差多少秒
int seconds = Seconds.secondsBetween(currentDateTime,targetDateTime).getSeconds();
// 相差多少周
int weeks = Weeks.weeksBetween(currentDateTime,targetDateTime).getWeeks();
4、获取零点相关的时间
DateTime currentDateTime = new DateTime();
// 今天的零点
DateTime dateTime = currentDateTime.withMillisOfDay(0);
// 昨天的零点
DateTime dateTime = currentDateTime.withMillisOfDay(0).plusDays(-1);
// 明天的零点
DateTime dateTime = currentDateTime.withMillisOfDay(0).plusDays(1);
// 这一年最后一天0点
new DateTime().dayOfYear().withMaximumValue().withMillisOfDay(0)
// 这一年第一天0点
new DateTime().dayOfYear().withMinimumValue().withMillisOfDay(0)
// 这个月最后一天0点
new DateTime().dayOfMonth().withMaximumValue().withMillisOfDay(0)
// 这个月月初0点
new DateTime().dayOfMonth().withMinimumValue().withMillisOfDay(0)
注意:要获取多少天后或者多少天前的零点,只需在plusDays()方法中填写相应参数即可
三、准确使用Joda Time的时间处理类
1、格式化就这么简单
// 格式化时间
DateTime currentDateTime = new DateTime();
currentDateTime.toString("yyyy-MM-dd HH:mm:ss");
// 指定时区格式化
String format = "yyyy-MM-dd HH:mm:ss";
DateTime dateTime = new DateTime();
dateTime.toString(format, Locale.US);
// 格式化时分秒(单位毫秒并且最大可格式23:59:59,超出将报错)
int millis = 120000;
LocalTime localTime = new LocalTime().withMillisOfDay(millis);
localTime.toString("HH:mm:ss");
2、 如果业务只需要日期,请使用LocalDate,因为LocalDate仅仅关心日期,更专业,也减少了不必要的资源消耗;如果业务只关心时间,那么使用LocalTime。例如:
LocalDate localDate = new LocalDate();
LocalTime localTime = new LocalTime();
System.out.println(localDate);
// 2017-09-14
System.out.println(localTime);
//10:54:14.506
3、 如果业务需要日期时间都要使用,那么可以使用LocalDateTime, DateTime这两个类,它们都是线程安全的同时都是不可变的,使用起来不用担心出问题。
LocalDateTime是与时区无关的。
DateTime是与时区相关的一个国际标准时间。
使用的时候根据自己的需要选择,详细的解释看官方文档吧!
4、再次提醒要使用DateTimeConstants类定义好的常量,避免重复造轮子。下面给出DateTimeConstants类的常量(也不多),不在解释,望名知义。
// 月份
public static final int JANUARY = 1;
public static final int FEBRUARY = 2;
public static final int MARCH = 3;
public static final int APRIL = 4;
public static final int MAY = 5;
public static final int JUNE = 6;
public static final int JULY = 7;
public static final int AUGUST = 8;
public static final int SEPTEMBER = 9;
public static final int OCTOBER = 10;
public static final int NOVEMBER = 11;
public static final int DECEMBER = 12;
// 星期
public static final int MONDAY = 1;
public static final int TUESDAY = 2;
public static final int WEDNESDAY = 3;
public static final int THURSDAY = 4;
public static final int FRIDAY = 5;
public static final int SATURDAY = 6;
public static final int SUNDAY = 7;
// 上午&下午
public static final int AM = 0;
public static final int PM = 1;
// 公元前...年(基督之前...年)
public static final int BC = 0;
// 公元前
public static final int BCE = 0;
// 公元...年(原义为主的纪年)
public static final int AD = 1;
// 基督纪元,公元
public static final int CE = 1;
// 1秒对应毫秒数
public static final int MILLIS_PER_SECOND = 1000;
// 1分钟对应秒数
public static final int SECONDS_PER_MINUTE = 60;
// 1分钟对应毫秒数
public static final int MILLIS_PER_MINUTE = 60000;
// 1小时对应分钟数
public static final int MINUTES_PER_HOUR = 60;
// 1小时对应的秒数
public static final int SECONDS_PER_HOUR = 3600;
// 1小时对应的毫秒数
public static final int MILLIS_PER_HOUR = 3600000;
// 1天对应的小时
public static final int HOURS_PER_DAY = 24;
// 1天对应的分钟数
public static final int MINUTES_PER_DAY = 1440;
// 1天对应的秒数
public static final int SECONDS_PER_DAY = 86400;
// 1天对应的毫秒数
public static final int MILLIS_PER_DAY = 86400000;
// 1周对应的天数
public static final int DAYS_PER_WEEK = 7;
// 1周对应的小时
public static final int HOURS_PER_WEEK = 168;
// 1周对应的分钟
public static final int MINUTES_PER_WEEK = 10080;
// 1周对应的秒数
public static final int SECONDS_PER_WEEK = 604800;
// 1周对应的毫秒数
public static final int MILLIS_PER_WEEK = 604800000;
Joda Time使用小结的更多相关文章
- 从零开始编写自己的C#框架(26)——小结
一直想写个总结,不过实在太忙了,所以一直拖啊拖啊,拖到现在,不过也好,有了这段时间的沉淀,发现自己又有了小小的进步.哈哈...... 原想框架开发的相关开发步骤.文档.代码.功能.部署等都简单的讲过了 ...
- Python自然语言处理工具小结
Python自然语言处理工具小结 作者:白宁超 2016年11月21日21:45:26 目录 [Python NLP]干货!详述Python NLTK下如何使用stanford NLP工具包(1) [ ...
- java单向加密算法小结(2)--MD5哈希算法
上一篇文章整理了Base64算法的相关知识,严格来说,Base64只能算是一种编码方式而非加密算法,这一篇要说的MD5,其实也不算是加密算法,而是一种哈希算法,即将目标文本转化为固定长度,不可逆的字符 ...
- iOS--->微信支付小结
iOS--->微信支付小结 说起支付,除了支付宝支付之外,微信支付也是我们三方支付中最重要的方式之一,承接上面总结的支付宝,接下来把微信支付也总结了一下 ***那么首先还是由公司去创建并申请使用 ...
- [Java]Java日期及时间库插件 -- Joda Time.
来到新公司工作也有一个多月了, 陆陆续续做了一些简单的项目. 今天做一个新东西的时候发现了 Joda Time的这个东西, 因为以前用的都是JDK原生的时间处理API, 大家都知道Java原生的时间处 ...
- iOS 之UITextFiled/UITextView小结
一:编辑被键盘遮挡的问题 参考自:http://blog.csdn.net/windkisshao/article/details/21398521 1.自定方法 ,用于移动视图 -(void)mov ...
- K近邻法(KNN)原理小结
K近邻法(k-nearst neighbors,KNN)是一种很基本的机器学习方法了,在我们平常的生活中也会不自主的应用.比如,我们判断一个人的人品,只需要观察他来往最密切的几个人的人品好坏就可以得出 ...
- scikit-learn随机森林调参小结
在Bagging与随机森林算法原理小结中,我们对随机森林(Random Forest, 以下简称RF)的原理做了总结.本文就从实践的角度对RF做一个总结.重点讲述scikit-learn中RF的调参注 ...
- Bagging与随机森林算法原理小结
在集成学习原理小结中,我们讲到了集成学习有两个流派,一个是boosting派系,它的特点是各个弱学习器之间有依赖关系.另一种是bagging流派,它的特点是各个弱学习器之间没有依赖关系,可以并行拟合. ...
随机推荐
- py+selenium+IE10【IE已停止工作】【已解决】
问题:跑自动化时,到某个用例IE就崩,提示已停止工作. 手工跑的时候,IE挂,提示“Internet Explorer 已经为了帮助保护您的计算机而关闭此网页”. 且每次都在需要调用flash插件 ...
- NOIP2018初赛题解 提高组
- 微信小程序踩坑日记2——实时访问数据库并渲染UI
0. 引言 主要讲讲对于实时访问数据库并渲染UI我的解决方法. 一开始查到了随让小程序是单线程的,但是有一个基本上是封装的worker线程,相当于可以自己自定义(类似于Android开发里的handl ...
- laravel 5.6初学笔记
laravel 5.6初学笔记 http://note.youdao.com/noteshare?id=bf4b701b49dd035564e7145ba2d978b4 框架简介 laravel文档齐 ...
- 分布式锁----Redis实现
分布式锁 为什么需要有分布式锁呢,在单点的时候synchronized 就能解决,但是服务拆分之后,每个服务都是单独的机器,无法解决,所以出现了分布式锁,其实也就是用各种手段,实现获取唯一锁,别人无法 ...
- LinkedList实现类
List还有一个LinkedList的实现,它是一个基于链表实现的List类,对于顺序访问集合中的元素进行了优化,特别是当插入.删除元素时速度非常快.因为LinkedList即实现了List接口,也实 ...
- 深入学习 Intellij IDEA 调试技巧
程序员的日常工作除了写代码之外,很大一部分时间将会在查找 BUG,解决问题.查找 BUG,离不开在 IDE 中调试代码.熟练的掌握调试技巧,可以帮助我们减少查找时间,快速定位问题. 在 IDEA 中调 ...
- Thinkphp5.0快速入门笔记(2)
学习来源与说明 https://www.kancloud.cn/thinkphp/thinkphp5_quickstart 测试与部署均在windows10下进行学习. 示例建立新的模块和控制器 在a ...
- C#实现Hash应用全解
1.引言 HASH是根据文件内容的数据通过逻辑运算得到的数值, 不同的文件(即使是相同的文件名)得到的HASH值是不同的. 通过一定的哈希算法(典型的有MD5,SHA-1等),将一段较长的数据映射为较 ...
- S2:c#继承
在C#中,如果一个类后面通过冒号又跟了另外一个类,那么我们就称冒号前面的类为子类,冒号后面的类为父类.这种书写类的方式放映出来的关系就称为类的继承关系. 1.子类:派生类 父类:基类或者超类 满足is ...