Wednesday 28 January 2015

DateFormat Conversion

Date Format conversion is a technique where you can convert any unformate date to formated date.

For eg:
Getting input as:

This can be done in simple way by using Simple Date Format
Always surrounds with try and catch when using Simple Date Format.

try {

String dateTimeString=2015-01-28 10:13:31;
SimpleDateFormat inputFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date=inputFormat.parse(dateTimeString);
SimpleDateFormat outFormat=new SimpleDateFormat("dd/MM/yyy");
String formated=outFormat.format(date);
Log.e("Formated", formated);
}
catch(Exception e){
ex.printStackTrace();
}

So based on SimpleDateFormat, you can convert to corresponding formats. For eg:

    new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH),
    new SimpleDateFormat("yyyyMMdd'T'HHmmss", Locale.ENGLISH),
    new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH),
    new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH),

No comments:

Post a Comment