Created
February 24, 2015 21:09
-
-
Save kevinmichaelchen/751f4279b4f747ebf597 to your computer and use it in GitHub Desktop.
Joda Utils
This file contains 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
package com.niupiao.niupiao.utils; | |
import org.joda.time.DateTime; | |
import org.joda.time.format.DateTimeFormat; | |
import org.joda.time.format.DateTimeFormatter; | |
import org.joda.time.format.ISODateTimeFormat; | |
/** | |
* Created by kevinchen on 2/23/15. | |
*/ | |
public class DateUtils { | |
public static final String TAG = DateUtils.class.getSimpleName(); | |
/** | |
* The format (US locale) of Rails DateTime objects, which corresponds to: | |
* "%Y-%m-%dT%H:%M:%S%Z" in Rails. | |
* <code>DateTime.parse(DateTime.now.iso8601).strftime("%Y-%m-%dT%H:%M:%S%Z")</code> | |
* <p/> | |
* Example: 2015-02-23T22:34:02-05:00 | |
*/ | |
public static final String FORMAT_RAILS_DATETIME = "yyyy-MM-d'T'HH:m:sZZ"; | |
/** | |
* Example: 10:34 PM | |
*/ | |
public static final String FORMAT_TIME = "h:mm a"; | |
/** | |
* Example: February 23, 2015 | |
*/ | |
public static final String FORMAT_DATE = "MMMMM d, y"; | |
/** | |
* Example: Monday | |
*/ | |
public static final String FORMAT_DAY = "EEEEE"; | |
/** | |
* | |
* @param isShort If true, "Feb". If false, "February". | |
*/ | |
public static String getMonth(DateTime dateTime, boolean isShort) { | |
DateTime.Property monthOfYear = dateTime.monthOfYear(); | |
return isShort ? monthOfYear.getAsShortText() : monthOfYear.getAsText(); | |
} | |
public static String getMonth(DateTime dateTime) { | |
return getMonth(dateTime, false); | |
} | |
public static String getDayOfMonth(DateTime dateTime) { | |
return dateTime.dayOfMonth().getAsText(); | |
} | |
public static String getYear(DateTime dateTime) { | |
DateTime.Property year = dateTime.year(); | |
return year.getAsText(); | |
} | |
public static String format(String dateTime, String format) { | |
return format(getDateTimeFromString(dateTime), format); | |
} | |
public static String format(DateTime dateTime, String format) { | |
DateTimeFormatter dtfOut = DateTimeFormat.forPattern(format); | |
return dtfOut.print(dateTime); | |
} | |
public static DateTime getDateTimeFromString(String railsRepresentation) { | |
DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateTimeNoMillis(); | |
return dateTimeFormatter.parseDateTime(railsRepresentation); | |
} | |
public static void main(String[] args) { | |
String dateTime = "2015-02-23T22:34:02-05:00"; | |
DateTime dt = getDateTimeFromString(dateTime); | |
System.out.println(format(dt, FORMAT_DAY)); | |
System.out.println(format(dt, FORMAT_DATE)); | |
System.out.println(format(dt, FORMAT_TIME)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment