jodatime

Jodatime date format

大憨熊 提交于 2019-12-22 09:12:09
问题 Is it possible to format JodaTime Date. Here is the code: private static LocalDate priorDay(LocalDate date1) { do { date1 = date1.plusDays(-1); } while (date1.getDayOfWeek() == DateTimeConstants.SUNDAY || date1.getDayOfWeek() == DateTimeConstants.SATURDAY); //System.out.print(date1); return date1; } Here date1 returns as: 2013-07-02 but i would like as 02-JUL-13 Thanks in advance 回答1: Is it possible to format JodaTime Date Yes. You want DateTimeFormatter. DateTimeFormatter formatter =

ISO8601 to DateTime with time zone information preserved

╄→尐↘猪︶ㄣ 提交于 2019-12-22 06:17:10
问题 Below is a deserialization of an ISO8601 date string that contains time zone information. Notice that the time zone information is lost: scala> val date1 = new DateTime().withZone(DateTimeZone.forID("Europe/Berlin")) date1: org.joda.time.DateTime = 2013-09-22T18:42:15.348+02:00 scala> date1.getZone() res45: org.joda.time.DateTimeZone = Europe/Berlin scala> val date2 = new DateTime(date1.toString()) date2: org.joda.time.DateTime = 2013-09-22T19:42:15.348+03:00 scala> date2.getZone() res46: org

ISO8601 to DateTime with time zone information preserved

拟墨画扇 提交于 2019-12-22 06:17:06
问题 Below is a deserialization of an ISO8601 date string that contains time zone information. Notice that the time zone information is lost: scala> val date1 = new DateTime().withZone(DateTimeZone.forID("Europe/Berlin")) date1: org.joda.time.DateTime = 2013-09-22T18:42:15.348+02:00 scala> date1.getZone() res45: org.joda.time.DateTimeZone = Europe/Berlin scala> val date2 = new DateTime(date1.toString()) date2: org.joda.time.DateTime = 2013-09-22T19:42:15.348+03:00 scala> date2.getZone() res46: org

How to compare two dates created as JodaTime LocalDate and LocalDateTime?

假装没事ソ 提交于 2019-12-22 05:35:33
问题 LocalDate startDate = new LocalDate(2014,1,2); LocalDateTime startDateTime = new LocalDateTime(2014,1,2,14,0); I need to compare startDate and startDateTime with respect to the date, something like this: // boolean equalDates = startDate.isequal(startDateTime.getDate()); Is it possible to do this? 回答1: If you just want to compare the date part, you can do it like so: LocalDate startDate = new LocalDate(2014, 1, 2); LocalDateTime startDateTime = new LocalDateTime(2014, 1, 2, 14, 0); LocalDate

JodaTime PeriodFormat, Elapsed time with only 1 Field

ε祈祈猫儿з 提交于 2019-12-22 05:26:11
问题 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. 回答1: There is no method in Joda-Time to do this, so you will have to test each field in turn manually. 回答2: this will work val startDate = new DateTime(millis).withZone(dateTimeZone) val endDate = new DateTime

Problem adding minutes with plusMinutes

拟墨画扇 提交于 2019-12-22 04:18:14
问题 I'm trying to add some minutes to a date using plusMinutes, but it just doesn't add anything at all: Here's the code: String currentDate ; SimpleDateFormat myFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm"); Date date1= null; DateTime dt; currentDate ="27/12/2010 11:29" ; try { date1= myFormat.parse(currentDate); } catch (ParseException ex) { ex.printStackTrace(); } dt = new DateTime(date1); dt.plusMinutes(30); 回答1: Javadoc says Returns a copy of this datetime plus the specified number of

Joda-time: First day in this year's ISO week 1

烈酒焚心 提交于 2019-12-22 03:21:15
问题 I would like to find out the date of the Monday in this year's ISO week 1 (For 2009 this would be Monday, Dec 29 2008). I'm sure that joda-time can handle this, but I just can't figure out the API (maybe it's just too late). Can anyone help? Thanks! 回答1: I believe this should work: DateMidnight date = new DateMidnight().withWeekOfWeekyear(1).withDayOfWeek(1); 回答2: The DateMidnight API mentioned in the other answer is now deprecated, use below: DateTime date = new DateTime().dayOfYear()

Joda-Time, Time without date

点点圈 提交于 2019-12-22 01:48:19
问题 I want a Class that only stores the time and not the date or day. Is there a class for this in Joda-Time ? or do I have to use a Date time and convert only the time part into a string and then use that part ? 回答1: There's the LocalTime class for that purpose. Read more about partials here. E.g.: LocalTime time = new LocalTime(12, 20); String formatted = time.toString("HH:mm"); 回答2: LocalTime - Immutable class representing a time without a date (no time zone) Check out this 回答3: Since JodaTime

Joda time, Period to total millis

隐身守侯 提交于 2019-12-22 01:33:39
问题 I'm trying to get the total amount of Milliseconds ( not the millis field) from the Period object instance. I've tried multiple conversions, as I couldn't find any method easily giving it. Has anyone ever needed that and managed to retrieve it ? (I need this for my patch, to figure out a negative period; negative millis = negative period.) 回答1: You can't get the millis directly from a Period , since fields like months and years are variable in terms of milliseconds. In order to make this work

How can I mock JodaTime actual date?

末鹿安然 提交于 2019-12-22 01:33:28
问题 I want to test this method: public FirmOrder findActiveByModelColor(ModelColor modelColor) { Query query = em.createQuery("FROM FirmOrder fo WHERE fo.modelColor = :modelColor AND fo.year = :year AND fo.month = :month"); query.setParameter("modelColor", modelColor); query.setParameter("year", new DateTime().year().get()); query.setParameter("month", new DateTime().monthOfYear().get()); return (FirmOrder) query.getSingleResult(); } but I need DateTime().year().get() and DateTime().dayOfMonth()