Skip to content

Instantly share code, notes, and snippets.

@nahba
Last active June 9, 2016 04:51
Show Gist options
  • Save nahba/8858945 to your computer and use it in GitHub Desktop.
Save nahba/8858945 to your computer and use it in GitHub Desktop.
Date Format Conversion
//Format
public static final String PLANNED_ = "dd-MMM-yyyy";
public static final String ENTRY_DATE = "dd/MM/yyyy HH:mm:ss";
//Using JodaTime
public String convertTo(final String passedDate, final String oldPattern, final String newPattern) {
String fullName = "";
DateTimeFormatter formatter = DateTimeFormat.forPattern(oldPattern);
DateTime jodaTime = formatter.parseDateTime(passedDate);
formatter = DateTimeFormat.forPattern(newPattern);
fullName = formatter.print(jodaTime);
return fullName;
}
//Using SimpleDateFormat
public String convertToFormat(final String passedDate, final String oldFormat, final String newFormat) {
String fullName = "";
DateFormat originalFormat = new SimpleDateFormat(oldFormat);
DateFormat targetFormat = new SimpleDateFormat(newFormat);
Date date;
try {
date = originalFormat.parse(passedDate);
fullName = targetFormat.format(date);
} catch (ParseException abhanException) {
fullName = "";
}
return fullName;
}
//Get 12/24 Hours Times - According To Device DateTime Settings
public String getTime() {
long timeInMillis = System.currentTimeMillis();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timeInMillis);
boolean is24HourFormat = DateFormat.is24HourFormat(context);
SimpleDateFormat format;
if(is24HourFormat) {
format = new SimpleDateFormat("EEE MMM dd yyyy, HH:mm");
} else {
format = new SimpleDateFormat("EEE MMM dd yyyy, hh:mm a");
}
return format.format(calendar.getTime());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment