Created
February 17, 2014 20:28
-
-
Save litil/9058455 to your computer and use it in GitHub Desktop.
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.urmoments.api.utils; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.Calendar; | |
import java.util.Date; | |
import java.util.Locale; | |
/** | |
* This class contains methods concerning the Date. It contains a method which | |
* calculates the age of a people from his birth date. | |
* | |
*/ | |
public class DateUtils { | |
private static SimpleDateFormat fullFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); | |
private static SimpleDateFormat nameOfDayFormat = new SimpleDateFormat("EEEE", Locale.ENGLISH); | |
private static SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm"); | |
/** | |
* This static methods calculate the age of a person from | |
* his birth date. | |
* | |
* @param birthDate Date object representing the Date of a person | |
* | |
* @return the age of the person, as a int | |
*/ | |
public static int calculateAgeFromDate(Date birthDate){ | |
int age = 0; | |
return age; | |
} | |
/** | |
* This method converts a Date object to a String as in this example: | |
* "Friday 6th at 16:39". | |
* | |
* @param date the Date object to convert | |
* | |
* @return the converted date as a String | |
*/ | |
public static String convertDateToStringDayAndHour(Date date){ | |
String ret = null; | |
// construct the Calendar instance | |
Calendar cal = Calendar.getInstance(); | |
cal.setTime(date); | |
// construct the parts of the final String | |
String dayOfWeek = nameOfDayFormat.format(date); | |
String dayOfMonth = Integer.toString(cal.get(Calendar.DAY_OF_MONTH)); | |
String time= timeFormat.format(date); | |
String daySuffix = ""; | |
switch (cal.get(Calendar.DAY_OF_MONTH)) { | |
case 1: | |
daySuffix = "st"; | |
break; | |
case 2: | |
daySuffix = "nd"; | |
break; | |
case 3: | |
daySuffix = "rd"; | |
break; | |
default: | |
daySuffix = "th"; | |
break; | |
} | |
ret = dayOfWeek + " " + dayOfMonth + daySuffix + " at " + time; | |
return ret; | |
} | |
/** | |
* This method converts a Date object to a String in the correct format. | |
* | |
* @param date the Date object to convert | |
* | |
* @return the converted date as a String | |
*/ | |
public static String convertDateToStringFull(Date date){ | |
String dateStr = null; | |
dateStr = fullFormat.format(date); | |
return dateStr; | |
} | |
/** | |
* This method converts a String to a Date. | |
* | |
* @param dateStr the date to convert | |
* | |
* @return the converted Date | |
* | |
* @throws ParseException | |
*/ | |
public static Date convertStringToDateFull(String dateStr) throws ParseException{ | |
Date date = null; | |
date = fullFormat.parse(dateStr); | |
return date; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment