37

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)?

Lii
  • 11,553
  • 8
  • 64
  • 88
user1097772
  • 3,499
  • 15
  • 59
  • 95
  • 4
    +1 for a Java date-related question that can't just be answered with "use Joda time". – GargantuChet Jan 15 '13 at 04:14
  • Have you taken look at joda time API. [Joda Time API link](http://joda-time.sourceforge.net/apidocs/org/joda/time/DateTimeZone.html). This will help you to resolve your issue. – Smit Jan 15 '13 at 04:26
  • This will also help you to sort your issue which was already asked on SO. [Using Joda time to get current wall time for a given Time Zone](http://stackoverflow.com/questions/9184499/using-joda-time-to-get-current-wall-time-for-a-given-time-zone) – Smit Jan 15 '13 at 04:31
  • @smit Yes, I read both of them before I asked but none of them helped me, cause I don't know how to set a timezone which I should probably use and I also don't know if I should set something with daylignt saving. Why they can't just make in documentation simple example "Here I am, whats the time?" – user1097772 Jan 15 '13 at 04:49
  • @user1097772 I understand. What are your requirements? What I mean is do you have specific date format to solve your issue? – Smit Jan 15 '13 at 05:43
  • @smit I need actual time for two things: 1) get timestamp for each post (when it was created) in format day_in_week day_in_month month year time(hours:minutes:seconds) 2) for computing the date of birth (user will have in his profile his date of birth) and form this two informations I'll compute the age. I don't thing there is problem with working with joda time to get 1) and 2). The only problem is to get correct current datetime which corresponds with timezone of my country. – user1097772 Jan 15 '13 at 06:11
  • @user1097772 Sorry for late reply. I added the answer, if you got any problem let me know. However answer given by llya is really good. – Smit Jan 15 '13 at 17:46
  • @smit thanks a lot, looks good I' gonna test it and let yok – user1097772 Jan 16 '13 at 10:59
  • @smit Hi, thanks a lot, sry for late response I had some stuf to be done. I've tryed your example and it helped me a lot to understand how does theese things works :) – user1097772 Jan 20 '13 at 01:06

2 Answers2

56

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
Smit
  • 4,685
  • 1
  • 24
  • 28
  • 2
    Hey man, this is great and really helpfull example, which explains how does it work. Thumbs up and thanks a lot :) – user1097772 Jan 20 '13 at 01:04
  • 1
    @ShajeelAfzal: The pseudo code I have provided is self-explanatory and its runnable if you apply correct jar files. Anyway I will look for this program (if its still sitting somewhere) and will try to update the answer with output. – Smit Mar 25 '15 at 23:01
  • 2
    "Here is pseudo Code" I do not think that word means what you think it means. –  Apr 01 '15 at 21:45
  • 3
    Above will produce following 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 – Sumit Arora Jun 28 '16 at 14:57
7
LocalTime localTime = new LocalTime();
LocalDate localDate = new LocalDate();
DateTime dateTime = new DateTime();
LocalDateTime localDateTime = new LocalDateTime();  

any of this contructor creates date in your timezone, where your timezone means time zone DateTimeZone.getDefault();

You want compare current date with date of birth. How do you save date of dirth in database?
If it is with UTC timezone, you can compare with dateTime in UTC TZ

Years.yearsBetween(dateOfBirth, new DateTime(DateTimeZone.UTC));   

If it has Server TZ you should do

Years.yearsBetween(dateOfBirth, new DateTime());  

If it has Server TZ you should do

Years.yearsBetween(dateOfBirth, new DateTime(DateTimeZone.forID("ClientTZ")));  
Ilya
  • 29,135
  • 19
  • 110
  • 158