Question

For getting Current date in mm/dd/yyyy format I am using the below code

SimpleDateFormat sdf = new SimpleDateFormat("mm/dd/yyyy");
                Date date = new Date();
                 String date3= sdf.format(date);
                date = sdf.parse(date3);

but everytime I print the date ,it gives me wrong and random output

output

Currentd Date:: 49/22/2013

output

Currentd Date:: 07/22/2013

Kindly suggest as what I should use to get current date. The Java Version I am using is 1.4

Was it helpful?

Solution

Change "mm/dd/yyyy" into "MM/dd/yyyy". m(lowercase) is use for minutes not for month. For month you should use M(uppercase)

OTHER TIPS

You might want to use MM instead of mm in the format pattern which will give you month instead of minutes.

Use

MM/dd/yyyy

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");


MM  -  Month
mm  -  Minute

m = Minute

M = Month

Thus you have to use "MM/dd/yyyy"

Try

Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
System.out.println(sdf.format(date));

Try

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime())); //2014/08/06 16:00:22
OR
Calendar cal = Calendar.getInstance();
System.out.println(cal.getTime()); //2014/08/06 16:00:22

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top