替代Java类的日期和时间【 Joda Time】
来源:互联网 发布:h5 js开发3d 编辑:程序博客网 时间:2024/04/30 19:57
其实在Joda Time学习的首页,就可以看到介绍,作为一个相对jdk 的date和calendar的更好的解决方案。joda-time在很多方面都支持的更多,例如:更加完整的时区支持,更易用的api接口。体现在日期的计算,比较和格式转换。
本文主要分享一些在开发过程中常用的joda-time的API以及几个示例。一些在笔者在处理oracle的timestamp的方案。
获取不同格式的时间
`System.out.println("Date格式:现在的时间是:"+new Date()); System.out.println("joda-time:现在的时间是:"+new DateTime()); System.out.println("joda-time:只获取年月日时间:"+(new DateTime()).toString("yyyy-MM-dd")); System.out.println("joda-time:今天是"+(new DateTime()).toString("E")); System.out.println("joda-time:获取时间到秒:"+(new DateTime()).toString("yyyy-MM-dd HH:mm:ss")); System.out.println("joda-time:获取时间到毫秒:"+(new DateTime()) .toString(" yyyy/MM/dd/ E HH:mm:ss.SSS")); System.out.println("joda-time:获取时间带下午:"+(new DateTime()).toString("MM/dd/yyyy hh:mm:ss.SSSa")); ;`
获取不同时区的时间
`System.out.println("joda-time:获取所在时区名称时间:"+(new DateTime()).toString("MM/dd/yyyy HH:mm ZZZZ")); System.out.println("joda-time:获取所在时区时间(依据格林威治时间标准):"+(new DateTime()).toString("MM/dd/yyyy HH:mm Z")+"(时区号)") //洛杉矶时间 DateTimeZone.setDefault(DateTimeZone.forID("America/Los_Angeles")); return new DateTime(DateTimeZone.forID(pattern));`
与calendar的转换
joda-time 仍然支持从calendar的方式转换为localtime。localtime提供默认的构造方法来转化。
Calendar calendar=Calendar.getInstance();
LocalTime localTime=new LocalTime(calendar);
System.out.println("由calendar转化为localtime的时间:"+localTime.toString());
计算时间
计算二者之间存在毫秒`Duration duration=new Duration(dateOne,dateTwo); return duration.getMillis();//某天是否存在于区间内 Interval i = new Interval(new DateTime("2012-02-01"), new DateTime("2012-04-01")); System.out.println( i.contains(new DateTime("2012-03-01")));` //得到明天 DateTime dt=new DateTime(); System.out.println(dt.plusDays(1));
其他
获取今天还剩多长时间`DateTime nowTime = new DateTime(); DateTime endOfDay = nowTime.millisOfDay().withMaximumValue(); return endOfDay.getMillis() - nowTime.getMillis();`获取一天开始的时间`DateTime nowTime = new DateTime(); DateTime startOfDay = nowTime.withTimeAtStartOfDay();`获取一天结束的时间`DateTime nowTime = new DateTime(); DateTime endOfDay = nowTime.millisOfDay().withMaximumValue();`
将oracle时间戳转化为long类型
`public static void timeStmap2Long(String strDate) throws ParseException { String tranDate = strDate.replace(".", ":").toString().replace(" ", ""); System.out.println("字符串长度为:" + tranDate.trim().length()); // target 日期pattern // 1.获取月份 String month = tranDate.substring(3, 4); if (tranDate.trim().length() == 26) { // 1.获取月份 month = tranDate.substring(3, 5); } System.out.println("月份为:" + month); // 2.获取天数 String day = tranDate.substring(0, 2); // 返回第二次出现"-"的索引 int twoindex = tranDate.lastIndexOf("-"); // 3.获取年份 String year = "20" + tranDate.substring(twoindex + 1, twoindex + 3); // 4.获取时间 // 取得上下午来判断小时 String time = tranDate.substring(twoindex + 3, twoindex + 12); // 取得上下午 String h = tranDate.substring(twoindex + 18, twoindex + 20); String rightTime = time; int intHHtime = 0; if ("下午".equals(h)) { String HHtime = tranDate.substring(twoindex + 4, twoindex + 5); System.out.println(HHtime + "小时"); intHHtime = Integer.parseInt(HHtime) + 12; rightTime = intHHtime + ":" + tranDate.substring(twoindex + 6, twoindex + 11); System.out.println(rightTime); } // 5.获取毫秒 String msec = tranDate.substring(twoindex + 12, twoindex + 18); int msecInt = Integer.parseInt(msec); String targetDate = month + "/" + day + "/" + year + " " + rightTime; SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); Date dt = sdf.parse(targetDate); long tarLong = dt.getTime() / 1000 + msecInt; System.out.println(tarLong);}`
long类型转换为datetime
这个直接由joda-time实现了一个构造函数来支持,简洁易用。` return new DateTime(datetime);`
诸如此类主要是DateTime、和localtime这两个类在起作用。
0 0
- 替代Java类的日期和时间【 Joda Time】
- 替代Java类的日期和时间【 Joda Time】
- Joda Time--jdk日期时间库的完美替代
- 强大易用的日期和时间库 Joda Time
- JODA-Time 日期与时间类库的学习
- [Java]Java日期及时间库插件 -- Joda Time.
- java时间操作类库:Joda-Time
- Java处理日期Joda-Time
- 【类库】java时间操作类库:Joda-Time
- Java日期计算之Joda-Time
- Java日期计算之Joda-Time
- Java日期计算之Joda-Time
- Java日期简化工具Joda-Time
- Java日期计算之Joda-Time
- Java|Joda-Time简化时间处理利器
- Java时间计算工具joda-time
- joda-time时间框架的使用
- joda.time 处理时间--计算2个日期之间的天数
- 常用10个LINUX命令说明
- ns-2与TCP拥塞模拟 之三 脚本语言OTcl
- Android ImageView的setImageLevel和level-list使用简介
- 283-e-Move Zeroes
- iOS开发------多线程编程(1)
- 替代Java类的日期和时间【 Joda Time】
- 大视野 1503 郁闷的出纳员 Splay 初步
- ns-2与TCP拥塞模拟 之四 ns-2仿真脚本中的元素
- POJ 1426 Find The Multiple(dfs)
- log4cpp
- 安卓简单开发(9)----打包APK
- R连接MySQL数据库方法
- [root@localhost zhu]# yum install vim Loaded plugins: langpacks, presto, refresh-packagekit Adding e
- 堆排序 Heap Sort