Convert string to date format

I need 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); 
+4
source share
5 answers

You need to parse the date string first (use DateFormat#parse() ) to get the Date using a format that matches the date string format.

And then format this Date object (use DateFormat#format() ), using the required format in SimpleDateFormat to get the 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); 

Result: -

 08 Dec 2012 19:09:57 

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

+8
source

change SimpleDateFormat to:

 String time = "2012-12-08 13:39:57 +0000"; DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z"); Date date = sdf.parse(time); DateFormat sdf = new SimpleDateFormat("dd MMM yyyy HH:mm:ss"); String formatedTime = sdf.format(date); System.out.println("Time: " + formatedTime); 
+4
source

Take a look at SimpleDateFormat. The code looks something like this:

 SimpleDateFormat fromUser = new SimpleDateFormat("dd/MM/yyyy"); SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd"); String reformattedStr = myFormat.format(fromUser.parse(inputString)); 
+3
source
 SimpleDateFormat simpleDateFormat=new SimpleDateFormat("dd-MM-yyyy"); Date date=simpleDateFormat.parse("23-09-2008"); 
+1
source

You can use the SimpleDateFormat class for this! eg:

 DateFormat mydate1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date1 = mydate1.parse(time); 
+1
source

All Articles