替代Java类的日期和时间【 Joda Time】
2015-10-06 21:32
671 查看
其实在Joda Time学习的首页,就可以看到介绍,作为一个相对jdk 的date和calendar的更好的解决方案。joda-time在很多方面都支持的更多,例如:更加完整的时区支持,更易用的api接口。体现在日期的计算,比较和格式转换。
本文主要分享一些在开发过程中常用的joda-time的API以及几个示例。一些在笔者在处理oracle的timestamp的方案。
诸如此类主要是DateTime、和localtime这两个类在起作用。
本文主要分享一些在开发过程中常用的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这两个类在起作用。
相关文章推荐
- struts1的html详细用法示例
- java笔记--java多线程
- Java三星题之剪刀石头布(三局两胜)
- 使用Spring配置事务时遇到的问题
- Java集合框架常用类源码分析与总结
- java内部类(三,嵌套类)
- Java基础知识强化之集合框架笔记45:Set集合之TreeSet存储自定义对象并遍历练习1(自然排序)
- struts2中实现表单内容的自动提交条件
- POJ 1001 Exponentiation(java+处理字符串)
- 深入理解Java:注解(Annotation)--注解处理器
- 破解myeclipse10找不到plugins文件
- 深入理解Java:注解(Annotation)基本概念
- 深入理解Java:注解(Annotation)自定义注解入门
- Java中关键字final用法
- Java版顺序表的浅拷贝与深拷贝(一)
- struts的国际化
- Integer与int的比较
- Myeclipse设置自动联想功能
- 默然日记20151006
- Java 使用Axis 开发WebService 客户端