Created
March 21, 2017 06:46
-
-
Save jaihind213/eb0fa6dd31c5be727a0ffd28eac41df6 to your computer and use it in GitHub Desktop.
date parsing using formatter.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.joda.time.DateTime; | |
import org.joda.time.DateTimeZone; | |
import org.joda.time.format.DateTimeFormat; | |
import org.joda.time.format.DateTimeFormatter; | |
public class DateParsing{ | |
public static void main(String[] args) { | |
System.setProperty("user.timezone", DateTimeZone.forOffsetHours(8).getID()); //singapore timezone | |
final String date = "2009-07-16T19:20:30-05:00"; | |
String pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'"; | |
System.out.println("***********************"); | |
System.out.println("imput date string :" +date); | |
System.out.println("imput date formatter patter :" +pattern); | |
System.out.println("system timezone :" + System.getProperty("user.timezone")); | |
System.out.println("***********************"); | |
System.out.println(); | |
System.out.println("------------------------------------------------ Z with quote"); | |
try { | |
DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern); | |
DateTime dateTime = dtf.parseDateTime(date); | |
System.out.println(dateTime); | |
} catch (Exception e) { | |
System.out.println(); | |
System.out.println("X X X X does not work: " + e.getMessage()); | |
System.out.println(); | |
} | |
System.out.println("------------------------------------------------ Z without quote"); | |
{ | |
pattern = "yyyy-MM-dd'T'HH:mm:ssZ"; | |
DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern); | |
DateTime dateTime = dtf.parseDateTime(date); | |
System.out.println(dateTime); | |
System.out.println(); | |
} | |
System.out.println("------------------------------------------------ Z without quote and offset parsed i.e. using timezone in date string"); | |
{ | |
pattern = "yyyy-MM-dd'T'HH:mm:ssZ"; | |
DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern).withOffsetParsed(); | |
DateTime dateTime = dtf.parseDateTime(date); | |
System.out.println(dateTime); | |
System.out.println(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment