Refine search
String dateTime = "11/15/2013 08:00:00"; // Format for input DateTimeFormatter dtf = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss"); // Parsing the date DateTime jodatime = dtf.parseDateTime(dateTime); // Format for output DateTimeFormatter dtfOut = DateTimeFormat.forPattern("MM/dd/yyyy"); // Printing the date System.out.println(dtfOut.print(jodatime));
String dateString = "2010-03-01T00:00:00-08:00"; String pattern = "yyyy-MM-dd'T'HH:mm:ssZ"; DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern); DateTime dateTime = dtf.parseDateTime(dateString); System.out.println(dateTime); // 2010-03-01T04:00:00.000-04:00
import org.joda.time.*; import org.joda.time.format.*; public class Test { public static void main(String[] args) { String text = "2011-03-10T11:54:30.207Z"; DateTimeFormatter parser = ISODateTimeFormat.dateTime(); DateTime dt = parser.parseDateTime(text); DateTimeFormatter formatter = DateTimeFormat.mediumDateTime(); System.out.println(formatter.print(dt)); } }
import org.joda.time.*; import org.joda.time.format.*; class Test { public static void main(String[] args) { parse("2002-10-10T12:00:00-05:00"); parse("2002-10-10T17:00:00Z"); } private static final DateTimeFormatter XML_DATE_TIME_FORMAT = ISODateTimeFormat.dateTimeNoMillis(); private static final DateTimeFormatter CHECKING_FORMAT = ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC); static void parse(String text) { System.out.println("Parsing: " + text); DateTime dt = XML_DATE_TIME_FORMAT.parseDateTime(text); System.out.println("Parsed to: " + CHECKING_FORMAT.print(dt)); } }
DateTimeFormatter parser = ISODateTimeFormat.dateTimeParser(); DateTime dateTime = parser.parseDateTime("2016-03-23T21:12:23+04:00"); System.out.println(dateTime); // 2016-03-23T18:12:23.000+01:00
DateTimeFormatter dt = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm"); System.out.println(dt.parseDateTime("12/17/2017 23:10"));
String s = "2014-01-15T14:23:50.026PST"; DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSz"); DateTime instant = dtf.parseDateTime(s); System.out.println(dtf.print(instant)); // abort Exception in thread "main" java.lang.IllegalArgumentException: Invalid format: "2014-01-15T14:23:50.026PST" is malformed at "PST" at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:866) at time.JodaTest8.main(JodaTest8.java:83)
String s = "2014-01-15T14:23:50.026"; DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSS"); DateTime instant = dtf.parseDateTime(s); System.out.println(dtf.print(instant)); // 2014-01-15T14:23:50.0260 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSS"); Date date = sdf.parse(s); System.out.println(sdf.format(date)); // 2014-01-15T14:23:50.0026 (bad!)
String string1 = "Oct 15, 2012 1:07:13 PM"; String string2 = "Oct 23, 2012 03:43:34 PM"; DateTimeFormatter dtf = DateTimeFormat.forPattern("MMM d, yyyy h:mm:ss a").withLocale(Locale.ENGLISH); DateTime dateTime1 = dtf.parseDateTime(string1); DateTime dateTime2 = dtf.parseDateTime(string2); Period period = new Period(dateTime1, dateTime2); PeriodFormatter formatter = new PeriodFormatterBuilder() .appendYears().appendSuffix(" years ") .appendMonths().appendSuffix(" months ") .appendWeeks().appendSuffix(" weeks ") .appendDays().appendSuffix(" days ") .appendHours().appendSuffix(" hours ") .appendMinutes().appendSuffix(" minutes ") .appendSeconds().appendSuffix(" seconds ") .printZeroNever() .toFormatter(); String elapsed = formatter.print(period); System.out.println(elapsed);
String time1 = "26-NOV-01 12.00.00.000000000 PM -07:00"; String time2 = "26-NOV-01 12.00.00.000000000 PM -08:00"; DateTimeFormatter parser = DateTimeFormat.forPattern("dd-MMM-yy hh.mm.ss.SSSSSSSSS aa Z").withZoneUTC(); // joda time date time formatter instance with a common UTC timezone System.out.println(parser.parseDateTime(time1)); // parse to date time - gives: 2001-11-26T19:00:00.000Z System.out.println(parser.parseDateTime(time2)); // parse to date time - gives: 2001-11-26T20:00:00.000Z System.out.println(parser.parseDateTime(time2).compareTo(parser.parseDateTime(time1))); // Comparing both the times here - gives: 1
String date = "2009-07-16T19:20:30-05:00"; String pattern = "yyyy-MM-dd'T'HH:mm:ssZ"; DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern); DateTime dateTime = dtf.parseDateTime(date); System.out.println(dateTime); // 2009-07-16T19:20:30-05:00
String date1 = "Apr 2010"; String date2 = "Jan 2009"; DateTimeFormatter formatter = DateTimeFormat.forPattern("MMM yyyy"); DateTime d1 = formatter.parseDateTime(date1); DateTime d2 = formatter.parseDateTime(date2); Months monthsBetween = Months.monthsBetween(d1, d2); System.out.println("Months diff: " + monthsBetween.get(DurationFieldType.months())); Years yearsBetween = Years.yearsBetween(d1, d2); System.out.println("Years diff: " + yearsBetween.get(DurationFieldType.years()));
public static void main(String[] args) { System.out.println(dateTimeFormat.parseDateTime("2014-05-25T16:45:49")); System.out.println(dateTimeAndTZFormat.parseDateTime("2014-05-25T16:45:49+00:00")); System.out.println(dateTimeAndTZFormat.parseDateTime("2014-05-25T16:45:49Z")); System.out.println(dateTimeWithSubSecFormat.parseDateTime("2014-05-25T16:45:49.100")); System.out.println(dateTimeWithSubSecAndTZFormat.parseDateTime("2014-05-25T16:45:49.000Z")); }
String input = "2014-04-10T00:00:00.000"; DateTimeZone timeZoneMontréal = DateTimeZone.forID( "America/Montreal" ); DateTimeFormatter formatter = ISODateTimeFormat.dateHourMinuteSecondFraction().withZone( timeZoneMontréal ); DateTime dateTime = null; try { dateTime = formatter.parseDateTime( input ); } catch ( IllegalArgumentException e ) { System.out.println( "Unexpected format of incoming date-time string: " + input + ". Exception: " + e ); // Handle exception for bad input. }
public static void main(String[] args) { DateTimeFormatter formatterC = DateTimeFormat.forPattern("HH:mm dd M YY").withZone(DateTimeZone.forID("Europe/Copenhagen")); System.out.println( formatterC.parseDateTime("19:30 29 8 11") ); }
String dateString = "2015-08-21 03:15:00+5:30"; String pattern = "yyyy-MM-dd HH:mm:ssZ"; DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern); DateTime dateTime = dtf.parseDateTime(dateString); System.out.println(dateTime);
String dateString = "2011-06-07T14:08:59.697-07:00"; String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"; DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern); DateTime dateTime = dtf.parseDateTime(dateString); System.out.println(dateTime);
DateFormat df = new SimpleDateFormat("HH:mm:ss"); String starttime="8:30:00"; String endtime="10:30:00"; DateTimeFormatter fmt = DateTimeFormat.forPattern("HH:mm:ss"); DateTime dateTime1 = fmt.parseDateTime(starttime); DateTime dateTime2 = fmt.parseDateTime(endtime); if (dateTime1.isBefore(dateTime2)){ System.out.println("awesome"); }
String text = "2011-10-02 18:48:05"; String text2 = "2011-10-02 18:50:05"; DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"); DateTime oldDate = formatter.parseDateTime(text); DateTime newDate = formatter.parseDateTime(text2); System.out.println(oldDate); System.out.println(newDate); Interval interval = new Interval(oldDate, newDate); System.out.println(interval);
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); DateTime dt = formatter.parseDateTime(dstDateTime.toString()); // You get a DateTime object // Create a new formatter with the pattern you want DateTimeFormatter formatter2 = DateTimeFormat.forPattern("dd-MMM-yy HH:mm:ss"); String dateStringInYourFormat = formatter2.print(dt); // format the DateTime to that pattern System.out.println(dateStringInYourFormat); // Prints 16-Jun-14 12:30:00 because of the TimeZone I'm in