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>

JodaTime用法简介的更多相关文章

  1. IOS NSInvocation用法简介

    IOS NSInvocation用法简介 2012-10-25 19:59 来源:博客园 作者:csj007523 字号:T|T [摘要]在 iOS中可以直接调用某个对象的消息方式有两种,其中一种就是 ...

  2. Apache自带压力测试工具ab用法简介

    ab命令原理 ab命令会创建很多的并发访问线程,模拟多个访问者同时对某一URL进行访问.它的测试目标是基于URL的,因此,既可以用来测试Apache的负载压力,也可以测试nginx.lighthttp ...

  3. Postman用法简介

    转自:http://blog.csdn.net/flowerspring/article/details/52774399 Postman用法简介 转载 2016年10月10日 09:04:10 10 ...

  4. MSSQL Sql加密函数 hashbytes 用法简介

    转自:http://www.maomao365.com/?p=4732 一.mssql sql hashbytes 函数简介 hashbytes函数功能为:返回一个字符,通过 MD2.MD4.MD5. ...

  5. java assert的用法简介【转】

    assert的基本用法 assertion(断言)在软件开发中是一种常用的调试方式,很多开发语言中都支持这种机制,如C,C++和Eiffel等,但是支持的形式不尽相同,有的是通过语言本身.有的是通过库 ...

  6. glVertexAttribPointer 用法简介

    在内存中采用交叉模式存储,向gpu传入顶点数据的方法  GPU: #version 100 attribute highp vec2 aPosition; attribute highp vec2 a ...

  7. C#中IPAddress类/Dns类/IPHostEntry类/IPEndPoint用法简介

    C#中IPAddress类/Dns类/IPHostEntry类/IPEndPoint用法简介 IP是一种普遍应用于因特网.允许不同主机能够相互找到对方的寻址协议.IP地址由4个十进制的数字号码所组成, ...

  8. Postman用法简介----https://blog.csdn.net/flowerspring/article/details/52774399

    https://blog.csdn.net/flowerspring/article/details/52774399 Postman用法简介

  9. SpringBoot系列之@Conditional注解用法简介

    SpringBoot系列之@Conditional注解用法简介 引用Spring官方文档的说法介绍一下@Conditional注解:Spring5.0.15版本@Conditional注解官方文档 @ ...

随机推荐

  1. Protected Member Access

    https://msdn.microsoft.com/en-us/library/bcd5672a.aspx 官方的说法The protected keyword is a member access ...

  2. Android安卓开发中图片缩放讲解

    安卓开发中应用到图片的处理时候,我们通常会怎么缩放操作呢,来看下面的两种做法: 方法1:按固定比例进行缩放 在开发一些软件,如新闻客户端,很多时候要显示图片的缩略图,由于手机屏幕限制,一般情况下,我们 ...

  3. JDBC 事务控制

    一.简介: 前面一遍提到了jdbc事务相关的概念.从中了解到事务应具有ACID特性.所以对于javaweb开发来说,某一个service层的方法,应该是一个事务,应该是具有原子性的.特别是当一个ser ...

  4. 类Item_equal

    class Item_equal: public Item_bool_func { List<Item_field> fields; /* list of equal field item ...

  5. UVA 1660 Cable TV Network 电视网络(无向图,点连通度,最大流)

    题意:给一个无向图,求其点连通度?(注意输入问题) 思路: 如果只有1个点,那么输出“1”: 如果有0条边,那么输出“0”: 其他情况:用最大流解决.下面讲如何建图: 图的连通度问题是指:在图中删去部 ...

  6. Buffer cache 的调整与优化

    Buffer cache 的调整与优化 -============================== -- Buffer cache 的调整与优化(一) --==================== ...

  7. 服务器安装Apache+Tomcat+Memcached共享Session的构架设计

    网站集群部署解决计划 一.       计划目标 实现互动留言系统.后台发布系统的高可用性,有效解决高并发量对单台应用服务器的打击,确保应用服务器单点故障不影响系统正常运行. 二.       部署架 ...

  8. linux面试题2

    1. 在Linux系统中,以文件的方式访问设备 2. Linux内核引导时,从文件 /etc/fstab 中读取要加载的文件系统 3. Linux文件系统中每个文件用inode来标识 4.  链接分为 ...

  9. yum install错误 系统环境:Oracle Linux5.4 在通过yum安装软件时出现以下错误:

    1.yum配置文件 1 [root@rh168 yum.repos.d]# cat yum.repo  2 [base] 3 name=Oracle linux  4 baseurl=file:/// ...

  10. Delphi 为什么它提示PCHAR是不安全的类型呢 Unsafe type 'PChar'

    DELPHI7已经考虑到了移植到点NET的问题,在点NET里,指针是不安全的.至于为什么有的人遇到,有的人没有遇到,那是因为各人的编译选项不同.在Project菜单下选Options“Compiler ...