Convert date into specific format

Go To StackoverFlow.com

-1

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?

2012-04-04 07:33
by dhananjay
possible duplicate of How do I parse a standard date into GMT?Andrew Thompson 2012-04-04 07:37


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.

2012-04-04 08:09
by Ravindra Gullapalli
Thanks,

Ravindra Gullapall - dhananjay 2012-04-04 09:24



3

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

2012-04-04 08:30
by Chaitu


1

Take a look here. You will need to use the SimpleDateFormat class.

2012-04-04 07:35
by npinti


1

Find some examples here.

  DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
  Date date = (Date)formatter.parse("01/29/02");
2012-04-04 07:38
by vkantiya
He wants to format in that forma - Jigar Joshi 2012-04-04 07:40
Ads