如何在Joda-Time获得正确的当前日期和时间?

时间:2021-10-27 21:31:21

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

如何在Joda Time获得正确的实际日期和时间?适当地,我指的是在我的国家的时间。我阅读了官方网页和一些教程 - 关于Locales和时区的内容很多,但我发现它很混乱。我没有找到任何例子如何简单地得到它。我需要它有两件事:1)在争论中获得最新信息2)获得当前时间,我将与出生日期“比较”并计算年龄。如何设置UTC + 1(布拉格 - 捷克*)时的当前时间?谢谢

2 个解决方案

#1


42  

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

这是Joda Time的伪代码,可能对您有用。

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

DateTimeFormat格式化程序的链接

Joda Time API

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.

P.S。:感谢Sumit Arora提供输出。

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

#2


5  

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();

任何此构造函数都在您的时区中创建日期,其中您的时区表示时区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

您想要将当前日期与出生日期进行比较。你如何在数据库中保存dirth的日期?如果是UTC时区,则可以与UTC TZ中的dateTime进行比较

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

If it has Server TZ you should do

如果它有Server TZ你应该这样做

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

If it has Server TZ you should do

如果它有Server TZ你应该这样做

Years.yearsBetween(dateOfBirth, new DateTime(DateTimeZone.forID("ClientTZ")));  

#1


42  

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

这是Joda Time的伪代码,可能对您有用。

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

DateTimeFormat格式化程序的链接

Joda Time API

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.

P.S。:感谢Sumit Arora提供输出。

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

#2


5  

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();

任何此构造函数都在您的时区中创建日期,其中您的时区表示时区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

您想要将当前日期与出生日期进行比较。你如何在数据库中保存dirth的日期?如果是UTC时区,则可以与UTC TZ中的dateTime进行比较

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

If it has Server TZ you should do

如果它有Server TZ你应该这样做

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

If it has Server TZ you should do

如果它有Server TZ你应该这样做

Years.yearsBetween(dateOfBirth, new DateTime(DateTimeZone.forID("ClientTZ")));