Back to project page journal.
The source code is released under:
MIT License
If you think the Android project journal listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package cochrane343.journal; //from w ww. j a v a2 s . c o m import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; /** * The helper class for handling dates and times. * It operates on Joda-Time's {@link DateTime}. * * @author cochrane34 * @since 1.0 */ public class DateTimeHelper { /** * The constant {@link DateTime} instance representing the Java epoch of 1970-01-01T00:00:00Z UTC */ private static final DateTime EPOCH = new DateTime(0, DateTimeZone.UTC); private static final int MONTHS_IN_ONE_YEAR = 12; private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormat.forPattern("MMM yy"); /** * @return a {@link DateTime} instance representing the current time in UTC */ public static DateTime getNow() { return new DateTime(DateTimeZone.UTC); } /** * @return the number of months elapsed since the Java epoch */ public static int getMonthsSinceEpoch() { final DateTime now = getNow(); return (now.year().get() - EPOCH.year().get()) * MONTHS_IN_ONE_YEAR + now.monthOfYear().get() - EPOCH.monthOfYear().get(); } /** * @param monthsSinceEpoch the number of months elapsed since the Java epoch * @return a {@link DateTime} instance representing the first timestamp of the month specified by <code>monthsSinceEpoch</code> */ public static DateTime getStartOfMonth(final int monthsSinceEpoch) { return getMonth(monthsSinceEpoch).dayOfMonth().withMinimumValue().withTimeAtStartOfDay(); } /** * @param monthsSinceEpoch the number of months elapsed since the Java epoch * @return a {@link DateTime} instance representing the last timestamp of the month specified by <code>monthsSinceEpoch</code> */ public static DateTime getEndOfMonth(final int monthsSinceEpoch) { return getStartOfMonth(monthsSinceEpoch).plusMonths(1).minusMillis(1); } /** * @param monthsSinceEpoch the number of months elapsed since the Java epoch * @return a print string for the the month specified by <code>monthsSinceEpoch</code> */ public static String printMonth(final int monthsSinceEpoch) { final DateTime month = getMonth(monthsSinceEpoch); return DATE_FORMATTER.print(month); } /** * @param monthsSinceEpoch the number of months elapsed since the Java epoch * @return a {@link DateTime} instance representing the month specified by <code>monthsSinceEpoch</code> */ public static DateTime getMonth(final int monthsSinceEpoch) { return EPOCH.plusMonths(monthsSinceEpoch); } }