Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Bug in SimpleDateFormat? Is that you also experienced?  RSS feed

 
ganesh roti
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following code created for to parse date and now it is creating questions ahead for me regarding java api’s parse method as you can see from the red line in log. It has changed the year to 53 while the given year is 2010. J







private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHH:mm");// 20100125 09:30:00.548





String input = sDay.trim() + tokens[1].trim();

Date dt = dateFormat.parse(input); // DateFormat creating a problem in high traffic

m_logger.debug(fileName+" line NO "+lineNum+" parsed date is "+ dt +" Date passed to parse is "+ input);









LOGS:



2010-03-30 16:03:23,588 DEBUG [pool-2-thread-4] (CurveLineThread.java:69) - TEG_20100224_tick.txt.gz line NO 55540 parsed date is Wed Feb 24 15:56:00 EST 2010 Date passed to parse is 2010022415:56:31.130

2010-03-30 16:03:23,588 DEBUG [pool-2-thread-4] (CurveLineThread.java:69) - TEG_20100224_tick.txt.gz line NO 55545 parsed date is Wed Feb 24 15:56:00 EST 2010 Date passed to parse is 2010022415:56:32.418

2010-03-30 16:03:23,588 DEBUG [pool-2-thread-4] (CurveLineThread.java:69) - TEG_20100224_tick.txt.gz line NO 55550 parsed date is Wed Feb 24 15:56:00 EST 2010 Date passed to parse is 2010022415:56:33.523

2010-03-30 16:03:23,588 DEBUG [pool-2-thread-4] (CurveLineThread.java:69) - TEG_20100224_tick.txt.gz line NO 55553 parsed date is Wed Feb 24 15:56:00 EST 2010 Date passed to parse is 2010022415:56:33.559

2010-03-30 16:03:23,588 DEBUG [pool-2-thread-4] (CurveLineThread.java:69) - TEG_20100224_tick.txt.gz line NO 55554 parsed date is Wed Feb 24 15:56:00 EST 2010 Date passed to parse is 2010022415:56:33.758

2010-03-30 16:03:23,588 DEBUG [pool-2-thread-4] (CurveLineThread.java:69) - TEG_20100224_tick.txt.gz line NO 55571 parsed date is Wed Feb 24 15:56:00 EST 2010 Date passed to parse is 2010022415:56:35.626

2010-03-30 16:03:23,588 DEBUG [pool-2-thread-2] (CurveLineThread.java:69) - TEG_20100224_tick.txt.gz line NO 55262 parsed date is Sat Feb 24 15:54:00 EST 53 Date passed to parse is 2010022415:54:47.392

2010-03-30 16:03:23,598 DEBUG [pool-2-thread-2] (CurveLineThread.java:69) - TEG_20100224_tick.txt.gz line NO 55572 parsed date is Wed Feb 24 15:56:00 EST 2010 Date passed to parse is 2010022415:56:35.847

2010-03-30 16:03:23,598 DEBUG [pool-2-thread-2] (CurveLineThread.java:69) - TEG_20100224_tick.txt.gz line NO 55581 parsed date is Wed Feb 24 15:56:00 EST 2010 Date passed to parse is 2010022415:56:37.252

2010-03-30 16:03:23,598 DEBUG [pool-2-thread-2] (CurveLineThread.java:69) - TEG_20100224_tick.txt.gz line NO 55602 parsed date is Wed Feb 24 15:56:00 EST 2010 Date passed to parse is 2010022415:56:41.157

2010-03-30 16:03:23,598 DEBUG [pool-2-thread-2] (CurveLineThread.java:69) - TEG_20100224_tick.txt.gz line NO 55619 parsed date is Wed Feb 24 15:56:00 EST 2010 Date passed to parse is 2010022415:56:50.866

 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is covered in the JavaDoc for SimpleDateFormat:
Date formats are not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally.


You can see discussion of alternative solutions here. Personally I like the Threadlocal solution, though it is a bit more complex-looking, and often you don't need that extra efficiency anyway.
 
ganesh roti
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!