String Date Conversion Java Example
These thing always should be handy or we should have some place where we can refer. Below is an example:
package com.datetest; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateFormatSample { public static final String DATE_FORMAT = "MM/dd/yyyy"; public static final String TIME_FORMAT = "MM/dd/yy hh:mm:ss.SSS a z"; public static final String LONG_TIME_FORMAT = "EEE MMM dd HH:mm:ss zzz yyyy"; public static void main(String[] args) throws ParseException { String date = "Tue Oct 18 12:39:35 EDT 2016"; // Its LONG_TIME_FORMAT as shown above SimpleDateFormat simpleDateFormat = new SimpleDateFormat(LONG_TIME_FORMAT); Date newDate = simpleDateFormat.parse(date); System.out.println("String to date converted :"+newDate); simpleDateFormat = new SimpleDateFormat(DATE_FORMAT); System.out.println("Date to string conversion: "+simpleDateFormat.format(newDate)); } }
- Output:
Reference: