Last active
August 29, 2015 13:56
-
-
Save slightfoot/9058079 to your computer and use it in GitHub Desktop.
Date time with TimeZone
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 java.text.SimpleDateFormat; | |
import java.util.Calendar; | |
import java.util.TimeZone; | |
import java.text.ParsePosition; | |
import java.text.DateFormat; | |
import java.util.Date; | |
public class DateTimeTest | |
{ | |
public static Calendar parseBookingDate(String bookingDate) | |
{ | |
Calendar cal = Calendar.getInstance(); | |
ParsePosition parsePos = new ParsePosition(0); | |
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); | |
sdf.setTimeZone(TimeZone.getTimeZone("UTC")); | |
Date date = sdf.parse(bookingDate, parsePos); | |
if(date == null){ | |
System.err.println("Error: " + parsePos); | |
return null; // new Date(); Current date time ?? | |
} | |
cal.setTime(date); | |
int zoneIndex = parsePos.getIndex(); | |
if(bookingDate.length() > zoneIndex){ | |
char zoneChar = bookingDate.charAt(zoneIndex); | |
if(zoneChar == '-' || zoneChar == '+'){ | |
cal.setTimeZone(TimeZone.getTimeZone("GMT" + | |
bookingDate.substring(zoneIndex))); | |
} | |
} | |
return cal; | |
} | |
public static String formatDateTime(Calendar cal) | |
{ | |
Date date = cal.getTime(); | |
DateFormat dateFormat = DateFormat | |
.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL); | |
dateFormat.setTimeZone(cal.getTimeZone()); | |
return dateFormat.format(date); | |
} | |
public static void main(String[] args) | |
{ | |
String inputDate = "2014-02-17T15:12:34-05:00"; | |
Calendar parsedDate = parseBookingDate(inputDate); | |
if(parsedDate != null){ | |
System.out.println("Booking Date: " + formatDateTime(parsedDate)); | |
} | |
} | |
} |
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 java.text.SimpleDateFormat; | |
import java.util.Calendar; | |
import java.util.TimeZone; | |
import java.text.ParsePosition; | |
import java.text.DateFormat; | |
import java.util.Date; | |
public class DateTimeTestAlt | |
{ | |
public static Calendar parseBookingDate(String bookingDate) | |
{ | |
Calendar cal = Calendar.getInstance(); | |
ParsePosition parsePos = new ParsePosition(0); | |
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); | |
sdf.setTimeZone(TimeZone.getTimeZone("UTC")); | |
Date date = sdf.parse(bookingDate, parsePos); | |
if(date == null){ | |
System.err.println("Error: " + parsePos); | |
return null; // new Date(); Current date time ?? | |
} | |
cal.setTime(date); | |
int zoneIndex = parsePos.getIndex(); | |
if(bookingDate.length() > zoneIndex){ | |
char zoneChar = bookingDate.charAt(zoneIndex); | |
if(zoneChar == '-' || zoneChar == '+'){ | |
cal.setTimeZone(TimeZone.getTimeZone("GMT" + | |
bookingDate.substring(zoneIndex))); | |
} | |
} | |
return cal; | |
} | |
public static String formatDateTime(Date date, TimeZone timeZone) | |
{ | |
DateFormat dateFormat = DateFormat | |
.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL); | |
dateFormat.setTimeZone(timeZone); | |
return dateFormat.format(date); | |
} | |
public static void main(String[] args) | |
{ | |
String inputDate = "2014-02-17T15:12:34-05:00"; | |
Calendar parsedDate = parseBookingDate(inputDate); | |
if(parsedDate != null){ | |
System.out.println("Booking Date: " + | |
formatDateTime(parsedDate.getTime(), parsedDate.getTimeZone())); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment