date and time conversion in java while reading a file

I am having problem with date and time conversion in java.

Actually i am reading a string from a file splinting that string and storing it to a string array, now initializing some variables with this array. my problem is I am saving this values to another file,while saving, the date format is changing i am posting my code.

string i am reading from file:

Wed May 02 12:07:54 EST 2012,Task 1 for  Test 1,20

code i had writen:

String s = br.readLine();
String[] list = s.split(",");
this.description = list[0];                
this.weight = Integer.parseInt(list[1]);
this.dueDate = new Date(list[2]);
//this.dueDate = dateFormat.parse(list[2]);
//SimpleDateFormat dateFormat = new SimpleDateFormat("E,dd MMM yyyy HH:mm.ss");
pw.println(description + "," + weight + "," + dueDate);

output of my code:

Thu May 03 03:07:54 EST 2012,Task 1 for  Test 1,20

Upvotes: 2

Views: 317

Answers (3)

vishu
vishu

Reputation: 5

Try using the .toString() method and convert your this.weight to String without parsing.

Upvotes: 0

alain.janinm
alain.janinm

Reputation: 20065

I disagree with @GrefHNZ answer because it doesn't work with list[0] = Wed May 02 12:07:54 EST 2012. Wed and May can't be parse in input.

I made this solution (works only for JDK7):

public static void main(String[] args)
    String s = "Wed May 02 12:07:54 EST 2012,Task 1 for Test 1,20";
    String[] list = s.split(",");
    String dateDesc = list[0];  
    String dateToParse = createStringToParse(dateDesc); //return "3 05 02 12:07:54 CEST 2012"
    SimpleDateFormat dateFormat = new SimpleDateFormat("u MM dd kk:mm:ss z yyyy");
    Date dueDate=null;
    try {
        dueDate = dateFormat.parse(dateToParse);
    } catch (ParseException ex) {
        Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.out.println(dueDate); //print "Wed May 02 12:07:54 CEST 2012"
 }
 private static String createStringToParse(String dateDesc) {
    int idx=dateDesc.indexOf(" ");
    String day = dateDesc.substring(0,idx);
    String month = dateDesc.substring(idx+1,dateDesc.indexOf(" ",idx+1));
    String rest = dateDesc.substring(dateDesc.indexOf(month)+month.length());
    switch(day){
        case "Mon" :
            day="1";break;
        case "Tue" :
            day="2";break;
        case "Wed" :
            day="3";break;
        case "Thu" :
            day="4";break;
        case "Fri" :
            day="5";break;
        case "Sat" :
            day="6";break;
        case "Sun" :
            day="7";break;
    }

    switch(month){
        case "Jan" :
            month="01";break;
        case "Feb" :
            month="02";break;
        case "Mar" :
            month="03";break;
        case "Apr" :
            month="04";break;
        case "May" :
            month="05";break;
        case "Jun" :
            month="06";break;
        case "Jul" :
            month="07";break;
        case "Aug" :
            month="08";break;
        case "Sep" :
            month="09";break;
        case "Oct" :
            month="10";break;
        case "Nov" :
            month="11";break;
        case "Dec" :
            month="12";break;
    }

    return day+" "+month+rest.replace("EST", "CEST");
}

To understand the SimpleDateFormat read the Oracle documentation.

Upvotes: 1

GregHNZ
GregHNZ

Reputation: 8979

You need to use the SimpleDateFormat to parse the input date.

Assuming that your input data is formatted as you've posted, (so the date is list[0]) put this in instead:

SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
this.dueDate = dateFormat.parse(list[0]);

The important thing being that you're telling it to check for the time zone (zzz). That should figure out the correct, actual, time that is described.

If you then just print out, it will (by default) print in your local timezone.

Upvotes: 1

Related Questions