Skip to content

Instantly share code, notes, and snippets.

@itsmejkumar
Created March 29, 2017 05:23
Show Gist options
  • Select an option

  • Save itsmejkumar/e87eae4b5b67572e1468466a89fc5faf to your computer and use it in GitHub Desktop.

Select an option

Save itsmejkumar/e87eae4b5b67572e1468466a89fc5faf to your computer and use it in GitHub Desktop.
Date and Time Formatting Android
private String getMonth(String date) throws ParseException {
Date d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH).parse(date);
Calendar cal = Calendar.getInstance();
cal.setTime(d);
String monthName = new SimpleDateFormat("MMM").format(cal.getTime());
return monthName;
}
private String getDate(String date) throws ParseException{
Date d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH).parse(date);
Calendar cal = Calendar.getInstance();
cal.setTime(d);
String dateName = new SimpleDateFormat("dd").format(cal.getTime());
return dateName;
}
private String getYear(String date) throws ParseException{
Date d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH).parse(date);
Calendar cal = Calendar.getInstance();
cal.setTime(d);
String yearName = new SimpleDateFormat("yyyy").format(cal.getTime());
return yearName;
}
private String getTime(String date) throws ParseException{
Date d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH).parse(date);
Calendar cal = Calendar.getInstance();
cal.setTime(d);
String timeName = new SimpleDateFormat("hh:mm a").format(cal.getTime());
return timeName;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment