Skip navigation.
Home

unexpected result of parsing date

salmo 3lykom i tried to parse adate in format -->dd/mm/yy

 but i get an unexpected result can you tell me why?

 

SimpleDateFormat df=new SimpleDateFormat("dd/MM/yy");

df.parse("02/10/06")

System.out.print(df.parse("02/10/06"));

//the result will be

Mon Oct 02 00:00:00 GMT+04:00 2006

while i expect

02/10/06

what's the problem

 

Parse converts a string to a Date object

Al salamo alikom wa rahmat allah wa barakatoh

The parse method of class SimpleDateFormat takes a String as input and returns a Date object represents the date in the input String

ie df.parse("1/1/2006"); will return a java.util.Date object with the time from 1/1/1970 to 1/1/2006 12:00:00:00

so when you parsed "02/10/06" you've got a java.util.Date object and when you passed that object to System.out.println(...), the Date.toString() method was called and that method returns the default representation for the Date object which will be "Mon Oct 02 00:00:00 GMT+04:00 2006"

and to get the result that you want, you need to use the format method on the Date object you just parsed [ie, System.out.println(df.format(df.parse("02/10/06")))], that will print "02/10/06" on the screen.

Al salamo alikom wa rahmat allah wa barakatoh

we 3lykom el salam we ra7mt

we 3lykom el salam we ra7mt alalh we barakto

thank yoy it's worked well
if i returned the result as string

but i need to return the result as adate

4 example

String Date=df.format(df.parse("02/10/06"))

print(Date);//will print 02/10/06

but i need to reciev the result in Data type date

Date date=new Date(df.format(df.parse("02/10/06")))
print (date);//will print

Thu Apr 10 00:00:00 GMT+04:00 2008

so what can i do?

 

Regards

we 3lykom el salam we ra7mt

we 3lykom el salam we ra7mt alalh we barakto

thank yoy it's worked well
if i returned the result as string

but i need to return the result as adate

4 example

String Date=df.format(df.parse("02/10/06"))

print(Date);//will print 02/10/06

but i need to reciev the result in Data type date

Date date=new Date(df.format(df.parse("02/10/06")))
print (date);//will print

Thu Apr 10 00:00:00 GMT+04:00 2008

so what can i do?

 

Regards

al salamo alikom wa rahmat

al salamo alikom wa rahmat allah wa barakatoh

the SimpleDateFormat class is used to convert String representations for Date/Time to java.util.Date objects and converting the java.util.Date objects to a String representation for the Date/Time stored in the Date object, and the java.util.Date class is used to store the time since 1/1/1970 12:00:00 until a specified time, it has nothing to do with the String representation for the stored date.

so If you want to convert the String "1/1/2006 01:30:00" to a java.util.Date object, you will use SimpleDateFormat.parse("..."), thing method will return a java.util.Date object contains the time from 1/1/1970 until 1/1/2006 01:30:00

java.util.Date t = simpleDateFormatObject.parse("1/1/2006 01:30:00");

and if you have a java.util.Date object contains the time from 1/1/1970 util 1/1/2006 and you want to convert it to a String contains the value "1 - 1 - 70", you will use the method SimpleDateFormat.format(dateObject).

String str = simpleDateFormatObject.format(theDateObject); // str will be "1 - 1 - 70" with the specified pattern

NOTE: the java.util.Date objects store the Date/Time in the form of Ticks, units smaller that mil.Secs.

back to the SimpleDateFormat class documentation in the javadocs to get more information about parsing and formatting dates and times.

al salamo alikom wa rahmat allah wa barakatoh

thank you very mush but if

thank you very mush

but if you have asolution 4 me it's will be very appriciated

thank you again

salmo 3laykom

Humm, here is a solution

al salamo alikom wa rahmat allah wa barakatoh

Why do you want to print the Date object on the screen without using SimpleDateFormat to convert it to a String ?

anyway, to do the required, make another class "MyDate" extends java.util.Date
and overide the method "toString()" and make that method returns the String represenation you want.

al salamo alikom wa rahmat allah wa barakatoh

we 3lykom el salm we ra7mt

we 3lykom el salm we ra7mt alalh

actually  i dont need to print the date on the screen as astring

but i need to read the string 12/02/06

and put in in adate

with the same format

Date date=new Date("12/02/06");

 

thank you man

thank you man

today i found aolution

DateFormat dateFormat = new SimpleDateFormat("dd/MM/yy");
java.util.Date issue = dateFormat.parse(request.getParameter("issuedate").toString());