如何在Joda-Time中获得正确的日期和时间? [英] How to get properly current date and time in Joda-Time?
问题描述
我需要它两件事:
1)获得当前的帖子,以诋毁
2)获取当前时间,我会比较与出生日期并计算年龄。
如何设置UTC + 1(布拉格 - 捷克共和国)的当前时间?
谢谢
这是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());
}
}
< a href =http://www.jarvana.com/jarvana/view/joda-time/joda-time/1.6.2/joda-time-1.6.2-javadoc.jar!/index.html = noreferrer> 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,
本地时间: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:亚洲/加尔各答
年差异:14
月差:173
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
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
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
这篇关于如何在Joda-Time中获得正确的日期和时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!