I am working on Java. I have a date for example: 20120328.
Year is 2012, month is 03 & day is 28.
I want to convert it into yyyy-MM-dd
format.
I have tried it but it is not working.
How to do it?
Asked
Active
Viewed 2.5k times
0

user unknown
- 35,537
- 11
- 75
- 121

dhananjay
- 321
- 2
- 11
- 20
-
1possible duplicate of [How do I parse a standard date into GMT?](http://stackoverflow.com/questions/1257542/how-do-i-parse-a-standard-date-into-gmt) – Andrew Thompson Apr 04 '12 at 07:37
5 Answers
5
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date date = new Date();
String ourformat = formatter.format(date.getTime());
If you want other than "MM/dd/yyyy" then just change the format in SimpleDateFormat
2
Find some examples here.
DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
Date date = (Date)formatter.parse("01/29/02");

Andrew Thompson
- 168,117
- 40
- 217
- 433

vkantiya
- 1,343
- 1
- 8
- 20
0
First you create the format using substring like
String myDate = "20120328";
String myConvertedDate = myDate.substring(0,4) + "-" + myDate.substring(5,6) + "-" + myDate.substring(7);
This produces the date as 2012-03-28
Then use simple date format to convert that into date if required.

Ravindra Gullapalli
- 9,049
- 3
- 48
- 70
0
You can use this one for Date formatting
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date date = new Date();
String ourformat = formatter.format(date.getTime());
And this one for getting TimeStamp
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Timestamp timestamp =timestamp=new Timestamp(System.currentTimeMillis());
String ourformat = formatter.format(timestamp);

Bruce wayne - The Geek Killer
- 390
- 2
- 18