jodatime

Joda-Time: Get first/second/last sunday of month

若如初见. 提交于 2019-12-19 17:44:14
问题 In plain Java, I have this code to get the last Sunday of the month. Calendar getNthOfMonth(int n, int day_of_week, int month, int year) { Calendar compareDate = Date(1, month, year); compareDate.set(DAY_OF_WEEK, day_of_week); compareDate.set(DAY_OF_WEEK_IN_MONTH, n); return compareDate; } // Usage Calendar lastSundayOfNovember = getNthOfMonth(-1, SUNDAY, NOVEMBER, 2012) What is a clean and elegant way to achieve the same result using Joda-Time? 回答1: public class Time { public static void

How to print out time zone abbreviations when using offset hours in Joda Time?

柔情痞子 提交于 2019-12-19 17:41:32
问题 I'm using Joda Time, and I'm being passed DateTimeZones that are created using DateTimeZone.forOffsetHours() . I'd like to print these timezones using standard timezone acronyms such as "PST", "EST", etc. However, whenever I print DateTimes that use these timezones, I get an "hh:mm" representation of the timezone instead of the name acronym. Here's an example: public class tmp { public static void main( String args[] ) { // "PST" System.out.println( DateTimeFormat.forPattern("z").print( new

How to covert Joda-Time's DateTimeFormat.forStyle() to JSR 310 JavaTime?

旧巷老猫 提交于 2019-12-19 11:27:12
问题 I working on convertion Grails Joda-Time plugin to JavaTime. And I've old Joda time code like this: def style switch (type) { case LocalTime: style = '-S' break case LocalDate: style = 'S-' break default: style = 'SS' } Locale locale = LocaleContextHolder.locale return DateTimeFormatter.ofPattern(style, locale).withResolverStyle(ResolverStyle.LENIENT) How can I conver it to JSR 310? I can't find anything similar to method forStyle(String style) which accepts style. UPD I found workaround:

String to joda LocalDate in format of “dd-MMM-yy”

自闭症网瘾萝莉.ら 提交于 2019-12-19 11:26:15
问题 I am using JAXB and joda time 2.2. to backup the data from Mysql to XML and restore it back. in my Table I have a Date attribute in format of "16-Mar-05". I successfully store this in XML. but when I want to read it from XML and put it back in Mysql table, I cant get the right format. this is my XMLAdapter class, here in unmarshal method the input String is "16-Mar-05", but I cant get the localDate variable in the format of "16-Mar-05", although I am setting pattern to "dd-MMM-yy". I posted

Android converting date time parse error (even tried joda time)

对着背影说爱祢 提交于 2019-12-19 09:26:23
问题 I'm parsing a number of news feeds and each item's pubDate follows the same format: Sun, 11 Jun 2017 18:18:23 +0000 Unfortunately one feed does not: Sat, 10 Jun 2017 12:49:45 EST I have tried to parse the date with no luck using androids java date and SimpleDateFormat : try { Calendar cal = Calendar.getInstance(); TimeZone tz = cal.getTimeZone(); SimpleDateFormat readDate = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"); readDate.setTimeZone(TimeZone.getTimeZone("UTC")); Date date =

How to convert Local time to am/pm time format using JodaTime?

為{幸葍}努か 提交于 2019-12-19 07:51:51
问题 I'm very new to Joda api and I have the call like this: LocalTime time = new LocalTime("13"); it prints as: 13:00:00.000 . I wish I could display it like this: 1:00 PM . How I can achieve that? Thanks in advance 回答1: Try the following: DateTimeFormatter builder = DateTimeFormat.forPattern("hh:mm:ss.SSa"); 回答2: I did this: LocalTime time = new LocalTime("13"); DateTimeFormatter fmt = DateTimeFormat.forPattern("h:mm a"); String str = fmt.print(time); And got this output for str: "1:00 PM" 来源:

Generating Random Date time in java (joda time)

↘锁芯ラ 提交于 2019-12-18 21:54:13
问题 Is it possible to generate a random datetime using Jodatime such that the datetime has the format yyyy-MM-dd HH:MM:SS and it should be able to generate two random datetimes where Date2 minus Date1 will be greater than 2 minutes but less than 60minutes. Please suggest some method. 回答1: This follows quite strictly what you asked for (except for the corrected format). Random random = new Random(); DateTime startTime = new DateTime(random.nextLong()).withMillisOfSecond(0); Minutes minimumPeriod =

Convert seconds since epoch to joda DateTime in Scala

£可爱£侵袭症+ 提交于 2019-12-18 19:07:24
问题 I am attempting to take seconds since epoch and turn it into a DateTime object in Scala. I am use joda. Unfortunately whether I use seconds or milliseconds, I'm getting weird results. What am I doing wrong here? scala> new org.joda.time.DateTime(1378607203*1000) res2: org.joda.time.DateTime = 1969-12-31T02:31:40.984Z scala> new org.joda.time.DateTime(1378607203) res3: org.joda.time.DateTime = 1970-01-16T22:56:47.203Z 回答1: Check a quick REPL session: scala> 1378607203 * 1000 res6: Int =

Jackson automatic formatting of Joda DateTime to ISO 8601 format

喜夏-厌秋 提交于 2019-12-18 10:39:35
问题 According to http://wiki.fasterxml.com/JacksonFAQDateHandling, “DateTime can be automatically serialized/deserialized similar to how java.util.Date is handled.” However, I am not able to accomplish this automatic functionality. There are StackOverflow discussions related to this topic yet most involve a code-based solution, but based upon the quote above I should be able to accomplish this via simple configuration. Per http://wiki.fasterxml.com/JacksonFAQDateHandling I have my configuration

Get minutes between two working days in Java

空扰寡人 提交于 2019-12-18 09:42:26
问题 I am trying to calculate the minutes between two Joda DateTimes or Calendars in Java. Ofcourse this would be simple if I could use the Minutes.minutesBetween function, but I can only count the minutes between working days and 8:00 AM - 6:00 PM. E.g.: Startdate: 03-11-2015 16:00 Enddate: 03-11-2015 17:00 This would be simple, as it would be exactly 60 minutes. Because it's a workday and between the given work hours. Now what I am trying to calculate is: Startdate: 03-11-2015 17:00 Enddate: 03