You can use SimpleDateFormat
to convert between a String
and a Date
object and vice versa based on a pattern. Click the API link, you'll see patterns being explained in detail. A 4-digit year can be represented with yyyy
, a 3-character month abbreviation can be represented with MMM
and so on.
First you need to parse the String
of the first format into a Date
object:
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date date = sdf1.parse(inputString);
Then you need to format the Date
into a String
of the second format:
SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
String outputString = sdf2.format(date);
Note that you need to take the Locale
into account as well to get the month to be printed in English, else it will use the platform's default locale to translate the month.