Android examples for java.util:Date Format
Converts a date from one format to another format
import android.util.Log; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; import java.util.TimeZone; public class Main{ private static final String TAG = TimeUtils.class.getName(); public static final TimeZone TIME_ZONE_UTC = TimeZone .getTimeZone("UTC"); /**// w w w . j av a 2 s. c om * Converts a date from one format to another format * * @param originalFormat the format of the original String * @param originalDate the original String * @param newFormat the desired format of the new String * @return the new String */ public static String convertDateTimeString(String originalFormat, String originalDate, String newFormat) { return createDateTimeString( parseStringToCalendar(originalDate, originalFormat), newFormat); } /** * Creates a date string according to a format string and a Calendar * * @param cal Calendar to convert to formatted string * @param formatString String specifying time format * @return a formatted date/time String */ public static String createDateTimeString(Calendar cal, String formatString) { SimpleDateFormat sdf = new SimpleDateFormat(formatString, Locale.US); // sdf.setTimeZone(TIME_ZONE_UTC); return sdf.format(calendarToDate(cal)); } /** * Creates a date string according to a format string and a TIME_ZONE_UTC Unix milliseconds time * * @param millis UTC Unix time in milliseconds to convert to formatted string * @param formatString String specifying time format * @return a formatted date/time String */ public static String createDateTimeString(long millis, String formatString) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(millis); SimpleDateFormat sdf = new SimpleDateFormat(formatString, Locale.US); // sdf.setTimeZone(TIME_ZONE_UTC); return sdf.format(calendarToDate(cal)); } /** * Creates a date string according to a format string and a Date * * @param date Date to convert to formatted string * @param formatString String specifying time format * @return a formatted date/time String */ public static String createDateTimeString(Date date, String formatString) { SimpleDateFormat sdf = new SimpleDateFormat(formatString, Locale.US); sdf.setTimeZone(TIME_ZONE_UTC); return sdf.format(date); } /** * Parse a date/time string of the specified format into a Calendar object * * @param timeString string to be parsed * @param format format of string to be parsed * @return a Calendar based on parsed string */ public static Calendar parseStringToCalendar(String timeString, String format) { return dateToCalendar(parseStringToDate(timeString, format)); } /** * Converts a Calendar to a Date * * @param cal calendar to convert * @return a date */ public static Date calendarToDate(Calendar cal) { return new Date(cal.getTimeInMillis()); } /** * Converts a Date to a Calendar * * @param date date to convert * @return a calendar */ public static Calendar dateToCalendar(Date date) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(date.getTime()); cal.setTimeZone(TimeZone.getDefault()); return cal; } /** * Parse a date/time string of the specified format into a Date object * * @param timeString string to be parsed * @param format format of string to be parsed * @return a Date based on parsed string */ public static Date parseStringToDate(String timeString, String format) { SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US); try { return sdf.parse(timeString); } catch (ParseException e) { Log.e(TAG, "Could not parse date string", e); return null; } } }