1

Possible Duplicate:
String to Date in Different Format in Java

Hi,

I m receiving date as String data type in MM/dd/yyyy format and need to convert in data Datatype in format yyyy-MM-dd to store in DB.

Wenfang Du
  • 8,804
  • 9
  • 59
  • 90
SHAHZZ
  • 101
  • 1
  • 1
  • 4

3 Answers3

2

You can use SimpleDateFormat:

SimpleDateFormat sourceFormat = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat destinationFormat = new SimpleDateFormat("yyyy-MM-dd");

String result = sourceFormat.format(destinationFormat.parse(input));
user756901
  • 176
  • 3
0

Use formatter for parsing the date to particular type

 String date="07/25/2011";
    SimpleDateFormat dateFormatter=new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date d=dateFormatter.parse(date);

            } catch (ParseException e) {
                  e.printStackTrace();
            }
Lalchand
  • 7,627
  • 26
  • 67
  • 79
0

You can also do it like this

String DATE_FORMAT_NOW = "dd-MM-yyyy HH:mm:ss";

//Instance of the calender class in the utill package
Calendar cal = Calendar.getInstance(); 

//A class that was used to get the date time stamp
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW); 

To print out the time you say

System.out.println(sdf.format(cal.getTime()) );

Cheers

DaMainBoss
  • 2,035
  • 1
  • 19
  • 26