Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

java - How to get properly current date and time in Joda-Time?

How to get properly actual date and time in Joda Time? By properly I mean time in my country. I read official pages and some tutorials - there are many things about Locales and timezones but I found it quite confusing. I did't found any example how to simply get it.

I need it for two things:

  1. To get current for post in disscusion,
  2. To get current time which I will "compare" with date of birth and compute the age.

How can I set the current time when I have UTC+1 (Prague - Czech Republic)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is pseudo Code for Joda Time which could be useful to you.

import org.joda.time.*;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class JodaTimeExample {

    public static void main(String[] sm) {
        DateTimeFormatter dateFormat = DateTimeFormat
                .forPattern("G,C,Y,x,w,e,E,Y,D,M,d,a,K,h,H,k,m,s,S,z,Z");

        String dob = "2002-01-15";
        LocalTime localTime = new LocalTime();
        LocalDate localDate = new LocalDate();
        DateTime dateTime = new DateTime();
        LocalDateTime localDateTime = new LocalDateTime();
        DateTimeZone dateTimeZone = DateTimeZone.getDefault();

        System.out
                .println("dateFormatr : " + dateFormat.print(localDateTime));
        System.out.println("LocalTime : " + localTime.toString());
        System.out.println("localDate : " + localDate.toString());
        System.out.println("dateTime : " + dateTime.toString());
        System.out.println("localDateTime : " + localDateTime.toString());
        System.out.println("DateTimeZone : " + dateTimeZone.toString());
        System.out.println("Year Difference : "
                + Years.yearsBetween(DateTime.parse(dob), dateTime).getYears());
        System.out.println("Month Difference : "
                + Months.monthsBetween(DateTime.parse(dob), dateTime)
                        .getMonths());
    }
}

Link for DateTimeFormat formatter

Joda Time API

I hope this will help you. If you got any question let me know.

P.S.: Thanks to Sumit Arora for giving the output.

dateFormatr : AD,20,2016,2016,26,2,Tue,2016,180,6,28,PM,8,8,20,20,25,20,2,??, 
LocalTime : 20:25:17.308 
localDate : 2016-06-28 
dateTime : 2016-06-28T20:25:18.872+05:30 
localDateTime : 2016-06-28T20:25:20.260 
DateTimeZone : Asia/Kolkata 
Year Difference : 14 
Month Difference : 173

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...