简体   繁体   中英

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);

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.

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);

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

SimpleDateFormat fromUser = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd");

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

You can use the SimpleDateFormat Class to do this! such:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM