joda的简单操作

在查阅了相关资料后, 总结了一些有关 joda 时间类的简单基本操作

直接看代码,按照基本需求  创建   解析  获得  设置 

导包 org.joda.time  大家也可以去他们的官网去查看  http://www.joda.org/joda-time/index.html# 

关于时间的操作  joda 可以帮助我们简化 

package edu.hdu.zhn;


import org.joda.time.DateTime;
import org.joda.time.DateTimeFieldType;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.junit.Test;

public class MyTest {

	@Test //创建时间的相关操作
	public void test1() throws Exception {
		DateTime today = new DateTime();
		//yyyy-MM-dd HH:mm:ss SSS表示毫秒数
		String sToday = today.toString("yyyy-MM-dd HH:mm:ss SSS");
		System.out.println(sToday);//2018-06-25 13:57:47 371

		//创建一个无时区时间
		DateTime today1 = new DateTime(DateTimeZone.UTC);
		//yyyy-MM-dd HH:mm:ss
		String sToday1 = today1.toString("yyyy-MM-dd HH:mm:ss");
		System.out.println(sToday1);//2018-06-25 05:57:47

		//创建一个指定时区时间
		DateTime today2 = new DateTime(DateTimeZone.forID("+08:00"));
		//		//yyyy-MM-dd HH:mm:ss
		String sToday2 = today2.toString("yyyy-MM-dd HH:mm:ss");
		System.out.println(sToday2);//2018-06-25 13:57:47
	}

	@Test //解析时间
	public void test2() throws Exception {

		//解析时间
		String sToday1 = "2018-06-25 13:54:58";
		DateTime today1 = DateTime.parse(sToday1, DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"));
		System.out.println(today1);	//2018-06-25T13:54:58.000+08:00

		//解析为UTC 时间
		String sToday2 = "2018-06-25 13:54:58";
		DateTime today2 = DateTime.parse(sToday2, DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"));
		DateTime utcToday2 = today2.toDateTime(DateTimeZone.UTC);
		System.out.println(utcToday2.toString("yyyy-MM-dd HH:mm:ss"));//2018-06-25 05:54:58
		System.out.println("UTC -> " + new DateTime(DateTimeZone.UTC).toString("yyyy-MM-dd HH:mm:ss"));//UTC -> 2018-06-25 05:56:29

		//解析为任意时区的时间
		String sToday3 = "2018-06-25 13:54:58";
		DateTime today3 = DateTime.parse(sToday3, DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"));
		DateTime zoneToday3 = today3.toDateTime(DateTimeZone.forID("+05:00"));
		//注意时间副本的概念  DateTime为一个不可变对象
		System.out.println(zoneToday3.toString("yyyy-MM-dd HH:mm:ss"));//2018-06-25 10:54:58
		System.out.println(today3.toString("yyyy-MM-dd HH:mm:ss"));//2018-06-25 13:54:58
	}
	
	@Test
	public void testGet() throws Exception {
		//操作时间获取信息  以下方式二选一, 看个人喜好而定
		DateTime today = new DateTime();
		System.out.println(today.toString("yyyy-MM-dd HH:mm:ss SSS"));//2018-06-25 14:03:28 342
		
		//1.按照get(DateTimeFieldType type) 获取时间类型
		System.out.println(today.get(DateTimeFieldType.dayOfMonth()));//25
		
		//2获取时按照语义即可,  getXxx(); 比较推荐 语义明了  
		int year = today.getYear();
		System.out.println(year);//2018
		int monthOfYear = today.getMonthOfYear();//6
		System.out.println(monthOfYear);
		int dayOfMonth = today.getDayOfMonth();//25
		System.out.println(dayOfMonth);
		int hourOfDay = today.getHourOfDay();//14
		System.out.println(hourOfDay);
		int minuteOfHour = today.getMinuteOfHour();
		System.out.println(minuteOfHour);//3
		int secondOfMinute = today.getSecondOfMinute();
		System.out.println(secondOfMinute);//28
		int millisOfSecond = today.getMillisOfSecond();
		System.out.println(millisOfSecond);//342
	}
	
	@Test
	public void testSet() throws Exception {
		DateTime today = new DateTime();
		System.out.println(today.toString("yyyy-MM-dd HH:mm:ss"));//2018-06-25 14:20:16
		//时间是不可变的, 所以更改的时间都是一个时间副本   + plus   - minus
		DateTime today2 = today.plusYears(1);
		System.out.println(today2.toString("yyyy-MM-dd HH:mm:ss"));//2019-06-25 14:20:16
		
		//推算   操作时间与 获取一样  plusXxx  minusXxx  
		DateTime today3 = today.plusYears(1).plusMonths(1).plusDays(1);
		System.out.println(today3.toString("yyyy-MM-dd HH:mm:ss"));//2019-07-26 14:20:16
		
		//指定时间 指定到 2月的最后一天
		DateTime today41 = today.monthOfYear().setCopy(2).dayOfMonth().withMaximumValue();
		System.out.println(today41.toString("yyyy-MM-dd HH:mm:ss"));//2018-02-28 14:20:16
		
		DateTime date2016 = new DateTime().year().setCopy(2016).monthOfYear().setCopy(2).dayOfMonth().withMaximumValue();
		DateTime date2018 = new DateTime().year().setCopy(2018).monthOfYear().setCopy(2).dayOfMonth().withMaximumValue();
		System.out.println(date2016.toString("yyyy-MM-dd"));//2016-02-29
		System.out.println(date2018.toString("yyyy-MM-dd"));//2018-02-28
		
	}

}

注   UTC时间协调世界时间,又称世界统一时间、世界标准时间、国际协调时间。

参考文章 : Joda Time项目和java8时间api

org.joda.time是一个开源的Java库,用于处理日期、时间时间区域的操作。它提供了许多实用的功能,使得在Java应用程序中处理日期和时间变得更加简单和灵活。 首先,org.joda.time库提供了许多日期和时间类,如DateTime、LocalDate、LocalTime和Period等。这些类代表了特定的日期、时间时间间隔,并提供了许多方法来执行各种操作,如计算两个日期之间的差异、添加或减去时间间隔等。 其次,org.joda.time还提供了丰富的格式化和解析日期时间的功能。它支持不同的字符串格式,可以将字符串解析为对应的日期时间对象,并可以将日期时间对象格式化为需要的字符串表示形式。这种灵活的格式化和解析功能非常有用,可以轻松地与数据库、文件和其他系统进行数据交换。 另外,org.joda.time库还提供了一些便捷的工具类,如DateTimeUtils和DurationUtils等。这些工具类提供了一些静态方法,可以进行一些常见的日期时间操作,如计算某日期是星期几、将时间戳转换为日期时间对象等。这些工具类可以方便地在项目中使用,减少了编写重复代码的工作量。 总之,org.joda.time是一个强大而且易于使用的日期和时间处理库。它提供了丰富的功能和灵活的接口,可以帮助开发人员更容易地处理日期和时间相关的逻辑。无论是在简单的日期计算还是在复杂的时间区域操作中,org.joda.time都是一个值得推荐的选择。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值