Joda Study

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

import org.joda.time.Chronology;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.Duration;
import org.joda.time.Interval;
import org.joda.time.LocalDate;
import org.joda.time.Months;
import org.joda.time.Period;
import org.joda.time.format.ISODateTimeFormat;


/**
 *
 * Joda API testing
 * core class : DateTime, Duration, Period, Duration, Interval and DateTime Calculation
 * @author Jason Lin
 *
 */
public class JodaLearning
{
    public static void main(String[] args)
    {
        //Define a time with special parameter
        DateTime dt = new DateTime(
                2013, //year
                7,    // month
                19,   // day
                15,   // hour (midnight is zero)
                50,   // minute
                40,   // second
                400   // milliseconds
        );
        
        //Define a current time  -- method 1
        DateTime currDT_1 = new DateTime();
        //Define a current time  -- method 2
        /*DateTime dateTime = SystemFactory.getClock().getDateTime();*/
        
        // DateTime calculation
        dt = dt.plusDays(5).plusMonths(1).dayOfWeek().withMaximumValue();
        
        System.out.println(dt.toString("E yyyy-MM-dd HH:mm:ss"));
        
        /**Joda API and JDK date API transfer   begin*/
        Calendar calendar = Calendar.getInstance();
        // DateTime to Calendar
        calendar.setTime(dt.toDate());
        // DateTime to JDK Date
        Date dateTime2JDKDate = dt.toDate();
        // Calendar to JDk Date
        Date calendar2JDKDate = calendar.getTime();
        // JDK date to DateTime
        DateTime dateTime_1 = new DateTime(new Date()); //JDK Date is not recommended
        DateTime dateTime_3 = new DateTime(calendar.getTime());
        
        DateTime dateTime_2 = new DateTime(new Date().getTime());
        /**Joda API and JDK date API transfer   end*/
        
        //LocalDate
        LocalDate now = currDT_1.toLocalDate();
        LocalDate electionDate = now.monthOfYear()
         .setCopy(11)        // November
         .dayOfMonth()       // Access Day Of Month Property
         .withMinimumValue() // Get its minimum value
         .plusDays(6)        // Add 6 days
         .dayOfWeek()        // Access Day Of Week Property
         .setCopy("Monday")  // Set to Monday (it will round down)
         .plusDays(1);       // Gives us Tuesday
        
        /**DateTime format  begin ****************/
        currDT_1.toString(ISODateTimeFormat.basicDateTime());
        currDT_1.toString(ISODateTimeFormat.basicDateTimeNoMillis());
        currDT_1.toString(ISODateTimeFormat.basicOrdinalDateTime());
        currDT_1.toString(ISODateTimeFormat.basicWeekDateTime());
        
        currDT_1.toString("MM/dd/yyyy hh:mm:ss.SSSa");
        currDT_1.toString("dd-MM-yyyy HH:mm:ss");
        currDT_1.toString("EEEE dd MMMM, yyyy HH:mm:ssa");
        currDT_1.toString("MM/dd/yyyy HH:mm ZZZZ");
        currDT_1.toString("MM/dd/yyyy HH:mm Z");
        /**DateTime format   end ****************/
        
        /**Duration , Period, Interval  begin*/
        DateTime start = new DateTime(2004, 12, 25, 0, 0, 0, 0);
        DateTime end = new DateTime(2006, 1, 1, 0, 0, 0, 0);
        
        Period period = new Period(start, end);
        
        Duration duration = new Duration(start, end);
        
        Interval interval = new Interval(start, end);
        DateTime start1 = interval.getStart();
        DateTime end1 = interval.getEnd();
        Chronology chrono = interval.getChronology();
        Duration dur = interval.toDuration();
        Period per = interval.toPeriod();
        
        Days days = Days.daysBetween(start, end);
        
        Months months = Months.monthsBetween(start, end);
        //System.out.println(period.getDays());
        /**Duration , Period, Interval  end*/
    }
}



  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值