Last active
August 29, 2015 14:07
-
-
Save Sunno/5a16c316d5a8e8c6fbbb to your computer and use it in GitHub Desktop.
Parse Dates from and to ISO date format
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
/** | |
* Joda Time libraries required! | |
* You can get them here https://github.com/JodaOrg/joda-time/releases | |
*/ | |
import org.joda.time.format.DateTimeFormatter; | |
import org.joda.time.format.ISODateTimeFormat; | |
import java.util.Calendar; | |
import java.util.Date; | |
/** | |
* Created by alvaro on 25/08/14. | |
*/ | |
public class DateTimeUtils { | |
static public DateTimeFormatter parser = ISODateTimeFormat.dateTimeParser(); | |
private DateTimeUtils(){ | |
} | |
/** | |
* Converts an ISO String into a Date object | |
* @param date ISO String | |
* @return converted Date | |
*/ | |
public static Date parse(String date){ | |
return new Date(parser.parseDateTime(date).getMillis()); | |
} | |
/** | |
* Converts a Date object into a ISO date String | |
* @param date Date object to convert | |
* @return String in ISO date format | |
*/ | |
public static String toString(Date date){ | |
DateTimeFormatter parser = ISODateTimeFormat.dateTime(); | |
return parser.print(date.getTime()); | |
} | |
/** | |
* Converts a Calendar object into a ISO date String | |
* @param date Calendar object to convert | |
* @return String in ISO date format | |
*/ | |
public static String toString(Calendar date){ | |
return toString(date.getTime()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment