How to convert String to Date and Time in Java

1 Answer

0 votes
package javaapplication1;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
 
public class JavaApplication1 {
 
    public static void main(String[] args) {
 
        SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMM dd, yyyy HH:mm:ss a", Locale.ENGLISH);
        String strDate = "Sunday, Oct 23, 2016 13:44:31 PM"; 
 
        try {
 
            Date date = formatter.parse(strDate);
            System.out.println(date);
            System.out.println(formatter.format(date));
 
        } catch (ParseException e) {
             System.out.println(e.getMessage());
        }
    }
}
  
/*
  
run:
  
Sun Oct 23 13:44:31 IDT 2016
Sunday, Oct 23, 2016 13:44:31 PM
  
*/

 



answered Oct 23, 2016 by avibootz
...