Reputation: 187
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
Upvotes: 1
Views: 334
Reputation: 14591
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.
Upvotes: 2
Reputation: 150
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)
Upvotes: 3