Joda-Time学习
2013-12-31 11:58
218 查看
Joda-Time学习
Joda-Time:
详细的介绍http://www.ibm.com/developerworks/cn/java/j-jodatime.html
实例:
直接上实例package com.mylearn.j2ee.jodatime; import org.joda.time.DateTime; import org.joda.time.DateTimeFieldType; import java.util.Calendar; import java.util.Date; import java.util.Locale; /** * Created by IntelliJ IDEA. * User: yingkuohao * Date: 13-12-31 * Time: 上午10:44 * CopyRight:360buy * Descrption: * http://www.ibm.com/developerworks/cn/java/j-jodatime.html * joda-time * To change this template use File | Settings | File Templates. */ public class JodaTimeTest { public static void main(String args[]) { //1.随便构造一个日期 DateTime dateTime = new DateTime(2000, 1, 1, 0, 0, 0, 0); System.out.println(dateTime.toString("yyyy-MM-dd HH:mm:ss")); //2.获取当前日期 DateTime now = DateTime.now(); System.out.println(now.toString("yyyy-MM-dd HH:mm:ss")); //3.根据当前日期操作,获取当前日期的一些属性 int hour = now.get(DateTimeFieldType.clockhourOfDay()); int dayOfYear = now.dayOfYear().get(); int dayOfMonth = now.dayOfMonth().get(); int dayOfWeek = now.dayOfWeek().get(); System.out.println("hour:--" + hour + "dayOfYear:--" + dayOfYear + "--dayOfMonth:--" + dayOfMonth + "--dayOfWeek:--" + dayOfWeek); int hourOfDay = now.hourOfDay().get(); int millisOfDay = now.millisOfDay().get(); int millisOfSecond = now.millisOfSecond().get(); int era = now.era().get(); System.out.println("--hourOfDay:--" + hourOfDay + "millisOfDay:--" + millisOfDay + "--millisOfSecond:--" + millisOfSecond + "--era--" + era); //4. 计算当前日期的前后 DateTime yesterday = now.minusDays(1); DateTime tommorrow = now.plusDays(1); System.out.println("yesterday:" + yesterday.toString("yyyy-MM-dd HH:mm:ss")); System.out.println("tommorrow:" + tommorrow.toString("yyyy-MM-dd HH:mm:ss")); //5. 计算当前日期1年前的1个月前一周的前一天前一小时前10分钟的前一秒时间 DateTime aDate = now.minusYears(1).minusMonths(1).minusWeeks(1).minusDays(1).minusHours(1).minusMinutes(10).minusSeconds(1); System.out.println("aDate:" + aDate.toString("yyyy-MM-dd HH:mm:ss")); //5.1与minus对应的plus方法,计算未来的时间 DateTime aFurtureTime = now.plusYears(1).plusMonths(1).plusWeeks(1).plusDays(1).plusHours(1).plusMinutes(10).plusSeconds(1); System.out.println("aFurtureTime:" + aFurtureTime.toString("yyyy-MM-dd HH:mm:ss")); //6. 日期的比较 boolean isBefore = now.isBefore(tommorrow); boolean isAfter = now.isAfter(yesterday); boolean isNow = now.isEqualNow(); System.out.println("isBefore=" + isBefore + ",isAfter=" + isAfter + "isNow=" + isNow); //7. 日期和java API的互操作性 Calendar calendar = now.toCalendar(Locale.getDefault()); System.out.println("calendare date:" + calendar.getTime()); Date curDate = now.toDate(); System.out.println("java.util.date=" + curDate); //8. 动态给日期赋值 DateTime curMonth = now.withMonthOfYear(10).withDayOfMonth(1).withHourOfDay(12).withMinuteOfHour(0).withSecondOfMinute(0); System.out.println("dateTime1:" + curMonth.toString("yyyy-MM-dd HH:mm:ss")); } } |
相关文章推荐
- Joda-time学习笔记
- JAVA学习14_Joda-Time 简介
- Joda-Time处理时间的学习(比calendar好用)
- JODA-Time 日期与时间类库的学习
- Java编程的逻辑 (33) - Joda-Time
- DateTime JodaTime 快捷方便
- Leetcode算法学习日志-309 Best Time to Buy and Sell Stock with Cooldown
- 【java】之joda-time的使用
- muduo学习笔记之time
- 图像抠图算法学习 - Shared Sampling for Real-Time Alpha Matting
- Joda.time.DateTime in Spring map to String as json restful response
- Python time时间模块学习
- 关于time_t类型的学习
- PHP学习笔记-->009 PHP Date / Time 函数
- 运行测试类学习Java中Time的API
- python标准库学习4-time
- 【算法导论学习-015】数组中选择第i小元素(Selection in expected linear time)
- Java8的日期API——joda time
- java学习——线程启动的两种方法,Time定时器,synchronized 的使用
- Python模块学习 ---- time 日期时间处理