Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert string into date format

I want this format 6 Dec 2012 12:10

  String time = "2012-12-08 13:39:57 +0000 ";

  DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
  Date date = sdf.parse(time);

  System.out.println("Time: " + date);
like image 944
Ketan Ahir Avatar asked Dec 04 '22 00:12

Ketan Ahir


1 Answers

You need to first parse your date string (Use DateFormat#parse() method) to get the Date object using a format that matches the format of date string.

And then format that Date object (Use DateFormat#format() method) using the required format in SimpleDateFormat to get string.

String time = "2012-12-08 13:39:57 +0000";

Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z").parse(time);
String str = new SimpleDateFormat("dd MMM yyyy HH:mm:ss").format(date);

System.out.println(str);

Output: -

08 Dec 2012 19:09:57

Z in the first format is for RFC 822 TimeZone to match +0000 in your date string. See SimpleDateFormat for various other options to be used in your date format.

like image 130
Rohit Jain Avatar answered Dec 25 '22 23:12

Rohit Jain