jodatime

Joda Time Interval between two dates including time zone

南笙酒味 提交于 2019-12-05 15:52:24
问题 I use JodaTime library for time operations. I have two dates: Date One: DateTime time_server = new DateTime(server_time_milisecs). withZone(DateTimeZone.forID("Europe/Zurich")); //+0100 Shows: 2013-01-27 13:44:42 Date two: DateTime time_local = new DateTime(DateTime.now()). withZone(DateTimeZone.getDefault()); //Have "Europe/Moscow" timezone +0400 Shows: 2013-01-27 16:41:47 I need to find real interval including timezone Interval interval = new Interval(time_local, time_server); Long.toString

Java ZonedDateTime to Instant conversion

梦想与她 提交于 2019-12-05 15:50:20
I am planning to convert ZonedDateTime to instant as per the below logic. Say, I am in PST timeZone and the current time is 11A.M. If I convert now( no daylight saving as of today March 04 2018) and the toInstant will be 7P.M. For the same 11 A.M, the toInstant will return 6 P.M as of April 04 2018 as daylight saving will be observed. So, The below code returns correctly. ZonedDateTime dateTime = ZonedDateTime.now(); --->>> March 04th 2018 at 11 A.M PST dateTime.plusMonths(1).toInstant(); -->> returns April 04th 2018 at 6 PM PST as daylight saving will be observed But, The result will be

Why DateTimeZone.getDefault() does not get updated when Zone in Android is changed

我的未来我决定 提交于 2019-12-05 15:28:55
I reviewed many questions related to TimeZones on Stackoverflow, but I could not find the one to the problem I am struggling with: Why doesn't Joda's DateTimeZone.getDefault() return updated timezone on TZ change (after resuming an application?). TimeZone.getDefault() seems to be working just fine . Should I use DateTimeZone.forTimeZone(TimeZone.getDefault()) to get up to date Joda's DateTimeZone object? Here is how to replicate: Start app that prints both DateTimeZone.getDefault() and TimeZone.getDefault() : 09-15 16:46:59.512 14961-14961/com.example.android.whatever D/TimeZone: DateTimeZone

Week between two dates Java + Joda Time

血红的双手。 提交于 2019-12-05 13:59:46
I wanted to get the number of weeks and months between two date range in Java. For ex., Start Date: 03/01/2012 End Date: 03/05/2012 Since the two dates fall in two different weeks I want the result to be 2 instead of 0. Second part of the problem is: Start Date: 02/29/2012 End Date: 03/01/2012 Number of months in between should be 2. I have been searching online regarding this and lot of people have recommended using Joda Date time in Java. So I gave it a shot. I was able to get the weeks working but I am not sure if this is the right way. Here is what I am doing to get the week duration:

Joda Time - difference in months between two dates [duplicate]

元气小坏坏 提交于 2019-12-05 13:46:28
This question already has an answer here: Number of days between two dates in Joda-Time 8 answers I need to get the difference in months between two dates, I'm using Joda Time, the problem is this: DateTime date1 = new DateTime().withDate(2015, 2, 1); DateTime date2 = new DateTime().withDate(2015, 1, 1); Months m = Months.monthsBetween(date1, date2); int monthDif = m.getMonths();//this return 0 it returns 0 because there is no month in the middle of the two dates, I need to return the difference in months not a few months in between, and add 1 would be problematic when the dates are the same.

java.util.date to String using DateTimeFormatter

痞子三分冷 提交于 2019-12-05 13:38:35
问题 How can I convert a java.util.Date to String using DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss") The Date object which I get is passed DateTime now = new DateTime(date); 回答1: If you are using Java 8, you should not use java.util.Date in the first place (unless you receive the Date object from a library that you have no control over). In any case, you can convert a Date to a java.time.Instant using: Date date = ...; Instant instant = date.toInstant();

Dozer mapping JodaTime property not working as expected

谁说我不能喝 提交于 2019-12-05 12:57:31
问题 I am using Dozer to map between a Document class to DocumentManagementBean class, both of my own making. Both have a property, with getters and setters, of Joda DateTime type, called dateAdded. When Document object d has property dateAdded =x, calling mapper.map(d, DocumentManagementBean.class) all fields get auto-mapped correctly (since I have full control over code base I am able to get away with no dozer-config and rely simply on matching property names), EXCEPT the dateAdded field, where

How to deserialize Joda DateTime using Jackson with Jersey 2 Client in Spring MVC?

ぃ、小莉子 提交于 2019-12-05 12:51:14
问题 I've been bashing my head with this proof of concept for a while. I want to consume a REST endpoint that returns JSON payload with an ISO8601 UTC timestamp: { ... "timestamp" : "2014-08-20T11:51:31.233Z" } and I want to consume it using a command line Java client written as a Jersey 2 client with Jackson/Spring Boot. The marshalling POJO is defined as follows: @JsonIgnoreProperties(ignoreUnknown = true) @JsonInclude(Include.NON_NULL) public class GreetingResource { @JsonProperty("timestamp")

Converting Unix timestamp to String with Joda Time

我的未来我决定 提交于 2019-12-05 08:55:44
问题 When trying to convert a Unix timstamp, from a database, to a String in a date format. int _startTS = evtResult.getInt("start"); //outputs 1345867200 Long _sLong = new Long(_startTS); //outputs 1345867200 //I've also tried: Long _sLong = new Long(_startTS*1000); //outputs 1542436352 DateTime _startDate = new DateTime(_sLong); //outputs 1970-01-16T08:51:07.200-05:00 The timestamp is for: Sat, 25 Aug 2012 . I have no idea why 1970 is always the output so hopefully someone can see a stupid

JodaTime PeriodFormat, Elapsed time with only 1 Field

人盡茶涼 提交于 2019-12-05 08:14:31
How can we display the elapsed time with only 1 field. For example Period = 1 Year 4 Months and 5 Days ==> Result = "1 Year ago" Period = 3 Months 5 Days ==> Result = "3 Months ago" Period = 4 Hours 5 Minutes ==> Result = "4 Hours ago" So i just want the highest available field. There is no method in Joda-Time to do this, so you will have to test each field in turn manually. this will work val startDate = new DateTime(millis).withZone(dateTimeZone) val endDate = new DateTime(dateTimeZone) val period = new Period(startDate, endDate) val localized = PeriodFormat.wordBased(localeLanguageCode)