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

use*_*772 34 java time datetime date jodatime

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

Smi*_*mit 47

这是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());
    }
}
Run Code Online (Sandbox Code Playgroud)

DateTimeFormat格式化程序的链接

Joda Time API

我希望这能帮到您.如果您有任何疑问,请告诉我.

PS:感谢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
Run Code Online (Sandbox Code Playgroud)

  • 嘿,这个很好,非常有帮助的例子,它解释了它是如何工作的.竖起大拇指,非常感谢:) (2认同)
  • "这是伪代码"我不认为这个词意味着你认为它意味着什么. (2认同)
  • 以上将产生以下输出:dateFormatr:AD,20,2016,2016,26,2,星期二,2016,180,6,28,PM,8,8,20,20,25,20,2 ,,当地时间: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年份差异:14个月差异:173 (2认同)

Ily*_*lya 5

LocalTime localTime = new LocalTime();
LocalDate localDate = new LocalDate();
DateTime dateTime = new DateTime();
LocalDateTime localDateTime = new LocalDateTime();  
Run Code Online (Sandbox Code Playgroud)

任何此构造函数都会在您的时区中创建日期,其中您的时区表示时区DateTimeZone.getDefault();

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

Years.yearsBetween(dateOfBirth, new DateTime(DateTimeZone.UTC));   
Run Code Online (Sandbox Code Playgroud)

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

Years.yearsBetween(dateOfBirth, new DateTime());  
Run Code Online (Sandbox Code Playgroud)

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

Years.yearsBetween(dateOfBirth, new DateTime(DateTimeZone.forID("ClientTZ")));  
Run Code Online (Sandbox Code Playgroud)