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流派,它的特点是各个弱学习器之间没有依赖关系,可以并行拟合. ...
随机推荐
- ZIP:ZipEntry
ZipEntry: /* 此类用于表示 ZIP 文件条目. */ ZipEntry(String name) :使用指定名称创建新的 ZIP 条目. ZipEntry(ZipEntry e) :使用从 ...
- [leetcode] 486. Predict the Winner (medium)
原题 思路: 解法一: 转换比较拿取分数多少的思路,改为考虑 player拿的分数为正,把Player2拿的视为负,加上所有分数,如果最后结果大于0则Player1赢. 思考得出递归表达式: max( ...
- [leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal (Medium)
原题 题意: 根据先序和中序得到二叉树(假设无重复数字) 思路: 先手写一次转换过程,得到思路. 即从先序中遍历每个元素,(创建一个全局索引,指向当前遍历到的元素)在中序中找到该元素作为当前的root ...
- vue项目目录结构详解
项目简介基于 vue.js 的前端开发环境,用于前后端分离后的单页应用开发,可以在开发时使用 ES Next.scss 等最新语言特性.项目包含: 基础库: vue.js.vue-router.vue ...
- 05-k8s调度器、预选策略、优选函数
目录 k8s调度器.预选策略.优选函数 节点选择过程 调度器 预选策略 优选函数 高级调度设置机制 node选择器/node亲和调度 pod亲和性 污点调度 Taints 与 Tolerations ...
- nginx lua集成kafka
NGINX lua集成kafka 第一步:进入opresty目录 [root@node03 openresty]# cd /export/servers/openresty/ [root@node03 ...
- python redis连接 有序集合去重
# -*- coding: utf-8 -*- import redisfrom constant import redis_ip, redis_db, redis_pw, logger, redis ...
- LDAP 服务搭建和后期管理
LDAP 服务 本文主要在debian配置,如果需要在CentOS上部署,需要修改大部分的路劲,这里需要自行修改. LDAP 服务按照个人理解,也可使理解为一个数据库,但是这个数据库的读写性能不像 M ...
- 邮件服务配置(虚拟域&虚拟用户)
邮件服务配置(虚拟域&虚拟用户) 现在我做的是: Linux + httpd + php + mariadb + postfix + dovecot + phpMyAdmin + postfi ...
- IT技术管理者的自我修养
1. 前言 本来写<IT技术管理者的自我修养>与<IT技术人员的自我修养>是一开始就有的想法.但发表<IT技术人员的自我修养>后,收到了不少良好的反馈,博客园的编辑 ...