File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Help!  Corrupted date needs to be restored. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Help!  Corrupted date needs to be restored." Watch "Help!  Corrupted date needs to be restored." New topic

Help! Corrupted date needs to be restored.

Kelly Dolan
Ranch Hand

Joined: Jan 08, 2002
Posts: 109
I want a date value to be stored in a database in the format 'yyyyMMddHHss'. Originally, I had a date value stored in this database as a long. At some point, the date value was retrieved from the database, parsed by a Java program as if it was in the format 'yyyyMMddHHss' (which was incorrect because it was a long, not of that format) and then saved back to the database. As a result, the date is corrupted. Now if I retrieve my date value and parse it as if it is in the format 'yyyyMMddHHss' (which it is now supposed to be), I see a date that's really really old when I should see a date that's really really recent.
My question is this? Is there anyway to take the date value that is currently stored in the database and convert it back to the original long value so that I can properly convert it? I included below a sample class I wrote to demonstrate what happened to my date value and to display the end result.
Many thanks to any help
package test;
import java.util.Date;
import java.text.SimpleDateFormat;
public class Test {
public Test() {
public static void main(String[] args) {
long time = System.currentTimeMillis();
// This is the value originally saved in the database.
System.out.println("The current date as a long is " + time);
Date date = new Date(time);
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmm");
// This is the value that should have been saved in the database if the date was properly converted.
System.out.println("The current date formatted is " + formatter.format(date));
Date newDate = null;
try {
newDate = formatter.parse(Long.toString(time));
// This is the value that was improperly converted and saved in the database.
System.out.println("The new date formatted is " + formatter.format(newDate));
} catch (Exception e) {
Date restoredDate = null;
try {
restoredDate = new Date(Long.parseLong(formatter.format(newDate)));
// This is an attempt to restore the current value to the original long value.
// This value should be the same as the variable 'time'.
System.out.println("The restored date as a long is " + restoredDate.getTime());
} catch (Exception e) {
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Can you spot any relationship between the 'yyyyMMddHHss' value you see on the corrupt date and the long value that was there? That might help you recreate a date object from the currently stored value.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Hope the following is what u want... One problem is that I'm not quite clear with the requirement explanation in your post...

Co-author of SCMAD Exam Guide, Author of JMADPlus
Kelly Dolan
Ranch Hand

Joined: Jan 08, 2002
Posts: 109
Thanks for all of the suggestions.
I pretty much determined that a timestamp can not be recovered from the corrupted value. The primary reason for this is that when the milliseconds were incorrectly parsed as 'yyyyMMddHHss', the parsing process rolled time over. For example, if the milliseconds value was 1069154318263 and the parse routine thought yyyy=1069, MM=15, dd=43, HH=18 and mm=263, it appears it said 263 minutes is actually 4 hours and 23 minutes, 18+4 hours is 22 hours, 43 days is actually 1 month and 12 days, 15+1 months is actually 1 year and 4 months. This results in a new 'yyyyMMddHHss' timestamp of 107004122223 (where 1070 is 1069+1). In order to reverse this parse, one must know how much time was rolled over from one part of the timestamp to another.
I agree. Here's the link:
subject: Help! Corrupted date needs to be restored.
It's not a secret anymore!