simpleDateFormat and April month

  • Thread starter Nurettin Arslankaya
  • Start date
N

Nurettin Arslankaya

Hi,

possibly, i had a bug to report. Here is sample code to test :

public Date StrToDate(String DT) throws Exception {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd
HH:mm:ss");
return formatter.parse(DT);
}

When I test with fallowing data I got wrong result.

1980-04-01 00:00:00 returns 1980-04-01 01:00:00
1975-04-12 00:00:00 returns 1975-04-12 01:00:00

for the dates It works wrong for 1 hour. when you enter 1980-04-01
01:00:00 it works correct but if you enter 1980-04-01 00:30:00 it
returns also 1 hour later time.


possibly there are several dates that has problem. I am using jre 1.4.
Does anyone know why it resulting wrong.
 
P

Paul Lutus

Nurettin said:
Hi,

possibly, i had a bug to report. Here is sample code to test :

public Date StrToDate(String DT) throws Exception {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd
HH:mm:ss");
return formatter.parse(DT);
}

That is not all the code you are using in your test. Please post a complete,
short, working program.
When I test with fallowing data I got wrong result.

1980-04-01 00:00:00 returns 1980-04-01 01:00:00

Using what output routine?
1975-04-12 00:00:00 returns 1975-04-12 01:00:00

for the dates It works wrong for 1 hour. when you enter 1980-04-01
01:00:00 it works correct but if you enter 1980-04-01 00:30:00 it
returns also 1 hour later time.


possibly there are several dates that has problem. I am using jre 1.4.
Does anyone know why it resulting wrong.

No, no one knows. Why? You did not post your code.
 
F

Filip Larsen

Nurettin Arslankaya wrote
possibly, i had a bug to report. Here is sample code to test :

public Date StrToDate(String DT) throws Exception {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd
HH:mm:ss");
return formatter.parse(DT);
}

When I test with fallowing data I got wrong result.

1980-04-01 00:00:00 returns 1980-04-01 01:00:00
1975-04-12 00:00:00 returns 1975-04-12 01:00:00

for the dates It works wrong for 1 hour. when you enter 1980-04-01
01:00:00 it works correct but if you enter 1980-04-01 00:30:00 it
returns also 1 hour later time.

Try fix the timezone on your formatter (for instance to GMT) and see if
that helps.

Regards,
 
N

Nurettin Arslankaya

Hi,
here is the all code and execution result :
/* Code Starts here */
/* -------------------------------------------- */
package DateFormatTester;
import java.text.*;
import java.util.*;

public class dtfmTest {
public dtfmTest() {

}

public static void main(String[] args) {
dtfmTest dtfm = new dtfmTest();
System.out.println("Testing Dateformatter ");
System.out.println("----------------------");

try {
System.out.println(dtfm.DateToStr(dtfm.StrToDate("1980-04-01 00:00:00")));
System.out.println(dtfm.DateToStr(dtfm.StrToDate("1975-04-12 00:00:00")));
}
catch (Exception ex) {
}
}

public Date StrToDate(String DT) throws Exception {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return formatter.parse(DT);
}

public String DateToStr(Date dt) throws Exception {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return formatter.format(dt) ;
}

}

/* Execution result */
/* -------------------------------------------- */
/*

Testing Dateformatter
----------------------
1980-04-01 01:00:00
1975-04-12 01:00:00

*/
 
S

Sudsy

Nurettin said:
Hi,
here is the all code and execution result :
/* Execution result */
/* -------------------------------------------- */
/*

Testing Dateformatter
----------------------
1980-04-01 01:00:00
1975-04-12 01:00:00

*/

My results:

Testing Dateformatter
----------------------
1980-04-01 00:00:00
1975-04-12 00:00:00

So it would certainly seem to be something to do with your environment.
I note the package directive at the top of your code. Is it possible
that you've got a "stale" version sitting somewhere in your CLASSPATH?
BTW I'm running Java 1.4.2_02 under Linux.
 
P

Paul Lutus

Nurettin said:
Hi,
here is the all code and execution result :

/ ... snip code, thanks for posting it.
/* Execution result */
/* -------------------------------------------- */
/*

Testing Dateformatter
----------------------
1980-04-01 01:00:00
1975-04-12 01:00:00

*/

I compiled and ran your code, here is my result:

Testing Dateformatter
----------------------
1980-04-01 00:00:00
1975-04-12 00:00:00

I think there is something wrong with your environment. BTW, you are
throwing away any error messages that may be printed by your code. Very bad
idea. Do this instead:

catch (Exception ex) {
ex.printStackTrace();
}
 
N

Nurettin Arslankaya

Hi,

I found that, the problem is daylight saving.

Now can anyone say how to parse a date as is (without daylight
timezone differences etc.)
 
P

Paul Lutus

Nurettin said:
Hi,

I found that, the problem is daylight saving.

Explain, in full, how you detected the problem, and how you see the
connection between that and the conversion error.
Now can anyone say how to parse a date as is (without daylight
timezone differences etc.)

Do it as I did, with your unchanged code, at a time amd place where my
computer is honoring daylight time. In other words, I don't think you have
adequately researched the problem or its solution.
 
P

P.Hill

Nurettin said:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

You might learn something by adding zzz to your format and see what
if any DLS it is applying.

-Paul
 
N

Nurettin Arslankaya

My java version is below:

java version "1.4.1_03"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_03-b02)
Java HotSpot(TM) Client VM (build 1.4.1_03-b02, mixed mode)

The example that I wrote is only an example, not my complete code. My
complete code is huge, diffucult to understand and not focusing the
problem.

I am at GMT+2 timezone ANKARA/TURKEY. And now its daylight+1 time in
Turkey.

I think there is a logical error. Because I create an calendar object
with Y M D H M S, when I rerequest these values it gives different
values. It allowes data without any Timezone information and responses
with timezone data.

Whatever, I solved my problem by creating SimpleTimeZone object
without Daylight saving at GMT+0. Then set it default and set TimeZone
property of the SimpleDateFormat object. This runs without error. Now
I am testing my code against to errors.

thanks for helping
 
P

P.Hill

Nurettin said:
The example that I wrote is only an example, not my complete code. My
complete code is huge, diffucult to understand and not focusing the
problem.

So does the code you posted result in the wrong values on your machine?
I am at GMT+2 timezone ANKARA/TURKEY. And now its daylight+1 time in
Turkey.
I think there is a logical error. Because I create an calendar object
with Y M D H M S, when I rerequest these values it gives different
values. It allowes data without any Timezone information and responses
with timezone data.

Can you show THIS as code since that is NOT what you are doing in the
previous code you posted.

For example, if you set the time right now during DLS then switch
to date which is non-DLS there are certain scenerios which will
correctly make the hour value move by one hour.
Whatever, I solved my problem by creating SimpleTimeZone object
without Daylight saving at GMT+0. Then set it default and set TimeZone
property of the SimpleDateFormat object. This runs without error.

This manualy setting of the timezone of the SimpleDateForma (SDF)
does suggest that however you are creating SDFs they are coming up
with a surprising TZ.

As I suggested before when you debug dates and times, show the zzz or ZZZZ
to see what zone each SDF is using.

-Paul
 
P

P.Hill

P.Hill said:
For example, if you set the time right now during DLS then switch
to date which is non-DLS there are certain scenerios which will
correctly make the hour value move by one hour.

Here is an example:

import java.text.SimpleDateFormat;
import java.util.*;

public class HourJump {
public static final String FORMAT_8601 = "yyyy-MM-dd 'T' HH:mm:ss.S zzz";

static SimpleDateFormat localTime = new SimpleDateFormat( FORMAT_8601 );
static SimpleDateFormat gmtTime = new SimpleDateFormat( FORMAT_8601 );
static {
gmtTime.setTimeZone( TimeZone.getTimeZone( "GMT" ));
}


public static void main(String[] args) {
Calendar cal = new GregorianCalendar();
showTime( cal );
cal.set( 2004, Calendar.DECEMBER, 15);
showTime( cal );
}

static void showTime( Calendar cal ) {
System.out.println(
" The GMT Time is: " + gmtTime.format( cal.getTime() )
+
" The Local Time is: " + localTime.format( cal.getTime() )
);
}
}

The output is:
The GMT Time is: 2004-09-18 T 14:23:01.839 GMT \
The Local Time is: 2004-09-18 T 07:23:01.839 PDT
The GMT Time is: 2004-12-15 T 15:23:01.839 GMT \
The Local Time is: 2004-12-15 T 07:23:01.839 PST

Notice that by only moving the date by manipulating a calendar object,
I was able to make the GMT clock time move by one hour. That is because
the calendar I was using when I set was running on local time while
the display is using GMT time.

I would guess your problem is something close to this scenerio.

-Paul
 
A

Andrew Thompson

Can you show THIS as code since that is NOT what you are doing in the
previous code you posted.

I agree with Paul. I was about to write
that yesterday, but wanted to hear something
from the other posters to the thread (I have
not used date/time stuff much).

This problem is not done yet, Nurettin, I
suggest you let the more experienced people
test your current code on their machines.

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.lensescapes.com/ Images that escape the mundane
 
N

Nurettin Arslankaya

Hi,

For your information, the code for that i wrote for test purposes.
Generally we did not left blank at least we place a message box there.


/* the problematic part of my code is below */
import java.util.*;

public class XMLIslem()

public XMLIslem() {

}

public static Date MStrToDate(String Dt) throws ParseException {
Date trh;
String dts = Dt.trim();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd
HH:mm:ss");
SimpleTimeZone tz=new SimpleTimeZone(0,"TURKEY/ANKARA");
tz.setStartYear(3000);
formatter.setTimeZone(tz);
tz.setDefault(tz);
formatter.setTimeZone(TimeZone.getDefault());
if (dts.equals("")) {
return ZeroDate;
}
trh = formatter.parse(dts);

if (!dts.equals(formatter.format(trh))){
throw new ParseException(dts + " geçerli bir tarih değil.
",0);
}

return trh;
}
}
/*

the code above is throws exception because, the input text and
formatted text are different. "February 31" is parsed without any
error. But in other programming languages this text can throw error.
So I need to recheck formatted text aganist to wrong data. But this
test fails at daylight saving starts days. The code above is running
correct. Possibly startyear may not needed. this code is working
several places and operating systems (especially Windows based) we
distribute Java runtime 1.4 that I am using. The error occured two
times in previous. One at April, 1, 1980 and the other at April, 14,
1975.


*/

thanks for your interests.
 
P

P.Hill

Nurettin Arslankaya wrote:
Thanks for showing us code, this is much more informative:
public static Date MStrToDate(String Dt) throws ParseException {
Date trh;
String dts = Dt.trim();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd
HH:mm:ss");
SimpleTimeZone tz=new SimpleTimeZone(0,"TURKEY/ANKARA");

The first problem is that this is NOT a known Java timezone, but
is code which constructs an arbitrqary timezone with the name you provided.
This results in a 0:00 offset timezone.

For curious reasons mostly to do with fact there is NOT
any international convention for the name of timezones, your
choices are probably:
Europe/Istanbul
Asia/Istanbul
Both of the above are the same GMT offset and DLS (Daylight savings) change date.

see also TimeZone.getAvailableIDs()
see
http://bepp.8m.com/english/java_applets/time_zone_java_applet.htm

then use TimeZone.getTimeZone( "Asia/Istanbul" );

Try this code:
SimpleTimeZone tz=new SimpleTimeZone(0,"TURKEY/ANKARA");
System.out.println( "Tz savings: " + tz.getDSTSavings() );
System.out.println( "Tz name: " + tz.getDisplayName() );
TimeZone tz2 = TimeZone.getTimeZone("Asia/Istanbul");
System.out.println( "Tz savings: " + tz2.getDSTSavings() );
System.out.println( "Tz name: " + tz2.getDisplayName() );
SimpleTimeZone tz3 = new SimpleTimeZone(0,"Africa/Kingdom of Prestor John");
System.out.println( tz.hasSameRules(tz3) );

Notice that an imaginary entry for "The Kingdom of Prestor John"
results in a TZ the same as the one you created.
tz.setStartYear(3000);

What you have said here is that you DO NOT want to apply the DLS for many years
to come. Why?
formatter.setTimeZone(tz);
tz.setDefault(tz);

This sets the default timezone for all places where a timezone
is not specified but when one is needed.
It is really TimeZone.setDefault(tz), a static call.
I doubt you want do that, since this will effect all
future Calendars and TimeZones and SimpleDateFormats you create.
formatter.setTimeZone(TimeZone.getDefault());

Formatter.setTimeZone( tz ) would have been as good.
if (!dts.equals(formatter.format(trh))){
throw new ParseException(dts + " geçerli bir tarih değil.
",0);

My Turkish ain't so good, would you mind putting that in English next time? :)

/*

the code above is throws exception because, the input text and
formatted text are different. "February 31" is parsed without any
error. But in other programming languages this text can throw error.

If you want everthing to take only valid dates, set the calendar
in the SimpleDateFormat to strict

You can do this with

formatter.getCalendar().setLenient( false );

Maybe this was your original problem which you needed to solve.
I hope so.
So I need to recheck formatted text aganist to wrong data. But this
test fails at daylight saving starts days.

Start days only? I think you have many more dates on which the
problem occurs.

The code above is running
correct. Possibly startyear may not needed. this code is working
several places and operating systems (especially Windows based) we
distribute Java runtime 1.4 that I am using. The error occured two
times in previous. One at April, 1, 1980 and the other at April, 14,
1975.
thanks for your interests.

I'm still interested. Let me know how it turns out.

-Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top