1

I have a Timestamp format in a string attribute .When I format it is adding some extra minutes i can see deviation in minutes. Have any one faced.Please help me out here

String dddddd="2017-11-29 09:24:03.857921";
SimpleDateFormat  fullMonthFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
SimpleDateFormat  fullMonthFormat2 = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss.SSSSSS");
Date strd11;
strd11=fullMonthFormat1.parse(dddddd);
System.out.println("chec date"+strd11);

String aa=fullMonthFormat2.format(strd11);
System.out.println("aa date"+aa);

o/p
chec dateWed Nov 29 09:38:20 IST 2017
aa date29-Nov-2017 09:38:20.000921 
1

2 Answers 2

3

I think the problem are your milliseconds, 857921 milliseconds are equal to ~ 14 minutes (14,2986833), which seems to be the time that got added to your output (output is correct, your input String or pattern needs fixing if you dont want this behaviour)

Sign up to request clarification or add additional context in comments.

2 Comments

Tx is there anything i can do without touching the source string .
@VikramSrinivasan: See my other answer regarding LocalDateTime.
2

TestingTest's answers your question already. Yet, if you want a different behaviour which does not re-calculate the minute you could use java.time.LocalDateTime (you need Java 8 though). This class does not mess up the minutes like SimpleDateformat.

See the difference in the code snippet below:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class TimeChecks {

    public static void main(String[] args) throws ParseException {
        String dddddd = "2017-11-29 09:24:03.857921";
        SimpleDateFormat fullMonthFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
        SimpleDateFormat fullMonthFormat2 = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss.SSSSSS");
        Date strd11;
        strd11 = fullMonthFormat1.parse(dddddd);
        System.out.println("chec date" + strd11);

        String aa = fullMonthFormat2.format(strd11);
        System.out.println("aa date" + aa);

        LocalDateTime localDate = LocalDateTime.parse(dddddd, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS"));
        System.out.printf("Local date: %s%n", localDate);
    }
}

This prints out:

chec dateWed Nov 29 09:38:20 GMT 2017
aa date29-Nov-2017 09:38:20.000921
Local date: 2017-11-29T09:24:03.857921

As you can see local date time does not change the minutes - it keeps the milliseconds as is.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.