JodaTime用法简介

JodaTime用法简介

Java的Date和Calendar用起来简直就是灾难,跟C#的DateTime差距太明显了,幸好有JodaTime

本文简单罗列JodaTime的用法

package com.chzhao.jodatest;

import java.util.Calendar;
import java.util.Date;

import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.Duration;
import org.joda.time.Interval;
import org.joda.time.Months;
import org.joda.time.Period;
import org.junit.Test;

public class JodaDemo {

	@Test
	public void testVoid() {
		Date dt = this.newDate();
		System.out.println(dt);
		this.dateTime2String(new DateTime(dt));
		this.addTime();
		this.intervalTest();
		this.periodTest();
	}

	@Test
	public void testNewDate0() {
		JodaDemo demo = new JodaDemo();
		System.out.println(demo.newDate());
	}

	public Date newDate() {
		DateTime dt = new DateTime(2012, 12, 13, 18, 23, 55);
		dt = new DateTime(Calendar.getInstance());
		dt = new DateTime(new Date());
		return dt.toDate();
	}

	public void dateTime2String(DateTime dateTime) {

		String str2 = dateTime.toString("MM/dd/yyyy hh:mm:ss.SSSa");
		String str3 = dateTime.toString("dd-MM-yyyy HH:mm:ss");
		String str4 = dateTime.toString("EEEE dd MMMM, yyyy HH:mm:ssa");
		String str5 = dateTime.toString("MM/dd/yyyy HH:mm ZZZZ");
		String str6 = dateTime.toString("MM/dd/yyyy HH:mm Z");
		System.out.println(str2);
		System.out.println(str3);
		System.out.println(str4);
		System.out.println(str5);
		System.out.println(str6);
	}

	public void intervalTest() {
		DateTime start = new DateTime(2004, 12, 25, 0, 0, 0, 0);
		DateTime end = new DateTime(2005, 1, 1, 0, 0, 0, 0);
		Interval interval = new Interval(start, end);
		System.out.println(interval.toString());
	}

	public void periodTest() {
		DateTime start = new DateTime(2004, 12, 25, 0, 0, 0, 0);
		DateTime end = new DateTime(2006, 1, 1, 0, 0, 0, 0);

		// period of 1 year and 7 days
		Period period = new Period(start, end);

		// calc will equal end
		DateTime calc = start.plus(period);

		// able to calculate whole days between two dates easily
		Days days = Days.daysBetween(start, end);

		// able to calculate whole months between two dates easily
		Months months = Months.monthsBetween(start, end);
	}

	public void durationTest() {
		DateTime start = new DateTime(2004, 12, 25, 0, 0, 0, 0);
		DateTime end = new DateTime(2005, 1, 1, 0, 0, 0, 0);

		// duration in ms between two instants
		Duration dur = new Duration(start, end);

		// calc will be the same as end
		DateTime calc = start.plus(dur);
	}

	public void addTime() {
		DateTime dateTime1 = DateTime.parse("2012-12-03");
		dateTime1 = dateTime1.plusDays(30);
		dateTime1 = dateTime1.plusHours(3);
		dateTime1 = dateTime1.plusMinutes(3);
		dateTime1 = dateTime1.plusMonths(2);
		dateTime1 = dateTime1.plusSeconds(4);
		dateTime1 = dateTime1.plusWeeks(5);
		dateTime1 = dateTime1.plusYears(3);
		DateTime dt = new DateTime();
		DateTime year2000 = dt.withYear(2000);
		DateTime twoHoursLater = dt.plusHours(2);
		System.out.println(year2000);
		System.out.println(twoHoursLater);
		String monthName = dt.monthOfYear().getAsText();
		System.out.println(monthName);
		System.out.println(dateTime1.toString("MM/dd/yyyy hh:mm:ss.SSSa"));
	}
}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.chzhao</groupId>
	<artifactId>jodatest</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>jodatest</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>joda-time</groupId>
			<artifactId>joda-time</artifactId>
			<version>2.6</version>
		</dependency>

	</dependencies>
</project>

posted @ 2015-01-18 03:44  wardensky  阅读(1102)  评论(0编辑  收藏  举报