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流派,它的特点是各个弱学习器之间没有依赖关系,可以并行拟合. ...
随机推荐
- Single Thread Execution设计模式
public class Test { public static void main(String[] args){ // FlightSercurityTest.test(); // EatNoo ...
- 你可能不知道的github的秘密
github也可以使用快捷键 先举例子,如何快速查找项目中的文件? 只需要进入项目,并按下T键 在浏览代码时,如何快速跳到指定行? 只需要进入项目,并按下L键 下面是一些常用的快捷键 聚焦搜索栏 按下 ...
- 宽字符转窄字符CW2AEX<>(szAreaInfo,CP_UTF8)
CString szAreaInfo; CW2AEX<>(szAreaInfo,CP_UTF8); 最好能像上面这样转换,否则汉字就会转成乱码.
- Shiro认证时的密码比对
在前面一节<Shiro在Web环境下集成Spring的大致工作流程>的最后一步中提到由Shiro完整密码比对. 那么具体是怎么工作的? 1,既然shiro会把密码来进行比对,当然会调用 U ...
- VUE动态(自动)Loading【绑定到URL】,同页面多个Loading互不冲突
需求来源:当使用React时,使用 umi loading 很方便,页面对http请求发生改变时,也会自动改变loading的相关状态(true/false) 对VUE插件进行找寻,发现没找到合适内容 ...
- Kafka工作流程分析
Kafka工作流程分析 生产过程分析 写入方式 producer采用推(push)模式将消息发布到broker,每条消息都被追加(append)到分区(patition)中,属于顺序写磁盘(顺序写磁盘 ...
- Jsoup访问https网址异常SSLHandshakeException(已解决)
爬取网页遇到的目标站点证书不合法问题. 使用jsoup爬取解析网页时,出现了如下的异常情况. javax.net.ssl.SSLHandshakeException: sun.security.val ...
- .net持续集成sonarqube篇之sonarqube安装与基本配置
系列目录 Sonarqube下载与安装 Sonarqube下载地址是:https://www.sonarqube.org/downloads/下载版本有两个,一个是长期支持版,另一个是最新版,此处安装 ...
- nginx(一)
localtion的语法 已=开头表示精确匹配 如 A 中只匹配根目录结尾的请求,后面不能带任何字符串. ^~ 开头表示uri以某个常规字符串开头,不是正则匹配 ~ 开头表示区分大小写的正则匹配; ~ ...
- 神奇的 SQL 之 CASE表达式,妙用多多 !
前言 历史考试选择题:黄花岗起义第一枪谁开的? A宋教仁 B孙中山 C黄兴 D徐锡麟,考生选C. 又看第二题:黄花岗起义第二枪谁开的? 考生傻了,就选了个B. 接着看第三题:黄花岗起义中,第三枪谁开的 ...