jodatime

Week between two dates Java + Joda Time

十年热恋 提交于 2019-12-10 09:32:24
问题 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

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

怎甘沉沦 提交于 2019-12-10 09:29:00
问题 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

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

女生的网名这么多〃 提交于 2019-12-10 08:41:44
问题 This question already has answers here : Number of days between two dates in Joda-Time (8 answers) Closed 3 years ago . 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

(Simple)DateFormat that allow 24:00:00 and 00:00:00 as inputs

早过忘川 提交于 2019-12-10 04:22:20
问题 I've been looking for this for a while, with no success so far. Do you know if there's a "DateFormat" ish class, that will allow me to use "00:00:00" and "24:00:00" as input parameters (they're both midnight) but when called "getHour()" I'll get 0 or 24 as a result? Using "kk" will only allow me to have <1:24> range, meanwhile I'm looking for <0:24> range formatting 回答1: The value 24:00 is not represented in a LocalTime because it is strictly part of the next day. Consideration was given to

Migrating from Java Calendar to Joda Date Time

三世轮回 提交于 2019-12-10 02:38:21
问题 Previously, when I first design a stock application related software, I decide to use java.util.Date to represent date/time information of a stock. Later, I realize most of the methods in java.util.Date is deprecated. Hence, very soon, I refactor all my code to make use of java.util.Calendar However, there is 2 shortcomings I encounter. Construct java.util.Calendar is comparative slower than java.util.Date Within the accessors getCalendar method of Stock class, I need to clone a copy, as

Get the difference between two dates in hours

牧云@^-^@ 提交于 2019-12-10 00:03:19
问题 I'm trying to calculate the hour difference between two joda dates. val d1 = getDateTime1() val d2 = getDateTime2() Here is what I did: # attemp1 val hours = Hours.hoursBetween(d1, d2) // error - types mismatch # attempt2 val p = Period(d1, d2, PeriodType.hours()) // error - there is such a constructor p.getHours So how do I do this? 回答1: The type of getDateTime1 should be org.joda.time.DateTime . This woks fine: val d1: org.joda.time.DateTime = DateTime.now val d2: org.joda.time.DateTime =

Jackson, Retrofit, JodaTime deserialization

拈花ヽ惹草 提交于 2019-12-09 19:17:32
问题 I'm using those three libraries: retrofit, jackson and jodatime, and I'm trying somethings to deserialize my object when it comes from my rest api, but I could not figure out how to solve this, here goes the json returned by rest api: { "establishment": "Gold Ball Confraria", "idCardPayment": 0, "paymentDate": "/Date(1461208761970+0000)/", "total": 10 } and that is the class that I've tried to deserialize my object: public class PaymentHistoryItemApiResult { @JsonProperty("total") private

How to preserve time zone in Joda-Time timestamp?

こ雲淡風輕ζ 提交于 2019-12-09 16:53:22
问题 I'm parsing timestamps. They are forced to my local time zone (Europe/London) when I read them in. I want to preserve the original time zone offset instead. scala> val fmt = org.joda.time.format.ISODateTimeFormat.dateTimeNoMillis() scala> val t = fmt parseDateTime ("2012-04-16T23:00:45-04:00") t: org.joda.time.DateTime = 2012-04-17T04:00:45.000+01:00 scala> t.getDayOfMonth res2: Int = 17 scala> fmt print t res1: java.lang.String = 2012-04-17T04:00:45+01:00 In this example, a time stamp from

How do I parse a string like “-8y5d” to a Period object in joda time

ぐ巨炮叔叔 提交于 2019-12-09 16:13:52
问题 I want to parse strings like "8y5d" or "-6y144d" into a joda time period object. Thanks 回答1: Use the PeriodFormatterBuilder: PeriodFormatter yearsAndMonths = new PeriodFormatterBuilder() .printZeroRarelyFirst() .appendYears() .appendSuffix("y", "y") .printZeroRarelyLast() .appendDays() .appendSuffix("d", "d") .toFormatter(); System.out.println(yearsAndMonths.parsePeriod("8y5d").toDurationFrom(new DateTime()).getStandardDays()); 来源: https://stackoverflow.com/questions/11669255/how-do-i-parse-a

java.time ISO date format with fixed millis digits (in Java 8 and later)

那年仲夏 提交于 2019-12-09 14:40:16
问题 By default, the toString method of Instant uses the DateTimeFormatter.ISO_INSTANT formatter. That formatter won’t print the digits for fraction-of-second if they happen to be 0. java-time examples: 2015-10-08T17:13:07.589Z 2015-10-08T17:13:07Z Joda-Time examples (and what I'd expect from java.time): 2015-10-08T17:13:07.589Z 2015-10-08T17:13:07.000Z This is really frustrating to parse in some systems. Elasticsearch was the first problem I encountered, there's no pre-defined format that