Pergunta

I recieve a date String of 02.04.201406:26:06 which i convert to another date

public static final SimpleDateFormat SDF_DATE_TIME_RECEIVED = new SimpleDateFormat("dd-MM-yy HH:mm:ss");
static{
    SDF_DATE_TIME_RECEIVED.setTimeZone(TimeZone.getTimeZone("IST"));
}
SimpleDateFormat originalDateFormat = new SimpleDateFormat("dd.MM.yyyyHH:mm:ss");
originalDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = originalDateFormat.parse(dateString);
String newDateString = SDF_DATE_TIME_RECEIVED.format(date));

This performs correctly and gives me the date as 02-04-14 11:56:06

Now i use this newDateString to again produce two different formats as illustrated below:

SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yy");
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
Date date = SDF_DATE_TIME_RECEIVED.parse(newDateString);
String datePart = dateFormat.format(date);
String timePart = timeFormat.format(date);

Now the problem is on Production server for dateString mentioned above i got the output as:

newDateString = "04/02/2014 11:56:06"
datePart = "04/02/70"
timePart = "00:56:06"

I tried to recreate the scenario on dev environment but i got the correct results as:

newDateString = "04/02/2014 11:56:06"
datePart = "04/02/14"
timePart = "11:56:06"

Even on doing the same process on production environment by resending the request the correct output appears.

What could be going wrong here? This is not an isolated issue. It is happening for a lot of requests with incorrect date almost random.

Foi útil?

Solução

Internally SimpleDateFormat is stateful so making it static final does not help at all with the multithreading issue. If your code gets called by multiple requests the SDF_DATE_TIME_RECEIVED will give corrupt results. It is the first suspect for this problem (especially since the errors seem random), try changing it to a local variable.

Another idea: you didn't set any time zones on the dateFormat and timeFormat. It looks like you expect that to be Indian Standard Time (IST) so you should do:

public static void main(String[] args) throws Exception {

    SimpleDateFormat SDF_DATE_TIME_RECEIVED = new SimpleDateFormat("dd-MM-yy HH:mm:ss");
    SDF_DATE_TIME_RECEIVED.setTimeZone(TimeZone.getTimeZone("IST"));

    SimpleDateFormat originalDateFormat = new SimpleDateFormat("dd.MM.yyyyHH:mm:ss");
    originalDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

    String dateString = "02.04.201406:26:06";
    Date date1 = originalDateFormat.parse(dateString);
    String newDateString = SDF_DATE_TIME_RECEIVED.format(date1);

    System.out.println(newDateString);

    SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yy");
    dateFormat.setTimeZone(TimeZone.getTimeZone("IST"));
    SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
    timeFormat.setTimeZone(TimeZone.getTimeZone("IST"));

    Date date2 = SDF_DATE_TIME_RECEIVED.parse(newDateString);
    String datePart = dateFormat.format(date2);
    String timePart = timeFormat.format(date2);

    System.out.println("datePart=" + datePart);
    System.out.println("timePart=" + timePart);
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top