Reputation: 6738
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);
Upvotes: 4
Views: 2965
Reputation: 15973
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);
Upvotes: 4
Reputation: 2363
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);
Upvotes: 1
Reputation: 213233
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.
Upvotes: 8
Reputation: 1856
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));
Upvotes: 3
Reputation: 431
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("dd-MM-yyyy");
Date date=simpleDateFormat.parse("23-09-2008");
Upvotes: 1