Java: milliseconds parsed wrongly when converted from string to timestamp - java

I have trouble while parsing the given string into a timestamp.
The milliseconds is parsed wrongly or let me know if I am missing something.
I get a string from the request as:
String selectedTimeStamp = request.getParameter("selectTime");
System.out.println("selectedTimeStamp: "+selectedTimeStamp);
and then I use simpleDateFormat to parse and format the string:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
Date parsedDate = sdf.parse(selectedTimeStamp);
Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
System.out.println("createdTime: " +timestamp);
The output I get is:
selectedTimeStamp: 2016-07-04 21:09:47.66
createdTime: 2016-07-04 21:09:47.066
Not sure why the millisecond is converted from 66 to 066 ? It should be 660
any idea?

Let's break it down step-by-step
Your code(effectively)
String selectedTimeStamp = "2016-07-04 21:09:47.6";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
Date parsedDate = sdf.parse(selectedTimeStamp);
System.out.println("parsedDate: " + parsedDate);
Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
System.out.println("createdTime: " +timestamp);
I am assuming that these are the imports you have made:
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
The output of which is
parsedDate: Mon Jul 04 21:09:47 IST 2016
createdTime: 2016-07-04 21:09:47.006
So even though I passed 6 ms as argument, it got parsed to 600 ms, this is most likely due to the fact that we are using, java library for parsing date and sql library for time stamping it. The process for treating date objects is different in both the languages, hence the inconsistency. Also millisecond is treated with a number of 1000th precision(as 1000ms = 1s) therefore SQL automatically converts the Java stored 6 to 006 (or 66 to 066 in your case).
I simple workaround this problem will be to check selectedTimeStamp using selectedTimeStamp.length() - selectedTimeStamp.lastIndexOf('.') and concatenating the remaining zeroes, i.e. if it is 2 then 2 zeroes, if 3 then 1 zero, and if 4 then no zeroes.
This will give you correct result.
Add this after the String selectesTimeStamp = "2016-07-04 21:09:47.6" line
int x = selectedTimeStamp.length() - selectedTimeStamp.lastIndexOf('.');
if (x==2)
selectedTimeStamp += "00";
if (x==3)
selectedTimeStamp += '0';
Cheers!!!
P.S.: Changing SSS to SS will not work.

Related

Convert date format from php to Java?

I am working on a Streaming Android application which I have to convert some php codes to java.
How can I convert this date format from php to java?
$today = gmdate("n/j/Y g:i:s A");
This date format in php is interpreted like this:
n: Numeric representation of a month, without leading zeros
j: Day of the month without leading zeros
Y: A full numeric representation of a year, 4 digits
g: 12-hour format of an hour without leading zeros
i: Minutes with leading zeros
s: Seconds, with leading zeros
A: Uppercase Ante meridiem and Post meridiem - AM/PM
and the same date format in java is like this:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("M/d/yyyy h:mm:ss a");
String today = simpleDateFormat.format(new Date());
To get a new java.util.Date object from your PHP date string, in Java:
String phpDateString = "7/24/2016 12:21:44 am";
SimpleDateFormat sdf = new SimpleDateFormat("M/d/yyyy h:mm:ss a");
Date javaDate = sdf.parse(phpDateString);
System.out.println(javaDate);
System.out.println(sdf.format(javaDate));
Output:
Sun Jul 24 00:21:44 CEST 2016
7/24/2016 12:21:44 AM
OP's self-answer was very informative, but it had an error in the Java expression (it's lowercase h for am/pm hours) and didn't include code to actually parse the PHP string into a Java Date object, which was the original question.

Java 8 Timezone conversion when creating java.util.Date [duplicate]

This question already has answers here:
Java program to get the current date without timestamp
(17 answers)
Closed 6 years ago.
I want to create a Date object without a TimeZone (eg : 2007-06-21). Is this possible?
When I use the following method it prints like Thu Jun 21 00:00:00 GMT 2007
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
TimeZone timeZone = TimeZone.getTimeZone("GMT");
timeZone.setDefault(timeZone);
sdf.setTimeZone(timeZone);
Date pickUpDate = sdf.parse("2007-06-21");
System.out.println(pickUpDate);
If you want to format a date, you need to use DateFormat or something similar. A Date is just an instant in time - the number of milliseconds since the Unix epoch. It doesn't have any idea of time zone, calendar system or format. The toString() method always uses the system local time zone, and always formats it in a default way. From the documentation:
Converts this Date object to a String of the form:
dow mon dd hh:mm:ss zzz yyyy
So it's behaving exactly as documented.
You've already got a DateFormat with the right format, so you just need to call format on it:
System.out.println("pickUpDate" + sdf.format(pickUpDate));
Of course it doesn't make much sense in your sample, given that you've only just parsed it - but presumably you'd normally be passing the date around first.
Note that if this is for interaction with a database, it would be better not to pass it as a string at all. Keep the value in a "native" representation for as much of the time as possible, and use something like PreparedStatement.setDate to pass it to the database.
As an aside, if you can possibly change to use Joda Time or the new date/time API in Java 8 (java.time.*) you'll have a much smoother time of it with anything date/time-related. The Date/Calendar API is truly dreadful.
This is the toString() of the java.util.Date
public String toString() {
// "EEE MMM dd HH:mm:ss zzz yyyy";
BaseCalendar.Date date = normalize();
StringBuilder sb = new StringBuilder(28);
int index = date.getDayOfWeek();
if (index == gcal.SUNDAY) {
index = 8;
}
convertToAbbr(sb, wtb[index]).append(' '); // EEE
convertToAbbr(sb, wtb[date.getMonth() - 1 + 2 + 7]).append(' '); // MMM
CalendarUtils.sprintf0d(sb, date.getDayOfMonth(), 2).append(' '); // dd
CalendarUtils.sprintf0d(sb, date.getHours(), 2).append(':'); // HH
CalendarUtils.sprintf0d(sb, date.getMinutes(), 2).append(':'); // mm
CalendarUtils.sprintf0d(sb, date.getSeconds(), 2).append(' '); // ss
TimeZone zi = date.getZone();
if (zi != null) {
sb.append(zi.getDisplayName(date.isDaylightTime(), zi.SHORT, Locale.US)); // zzz
} else {
sb.append("GMT");
}
sb.append(' ').append(date.getYear()); // yyyy
return sb.toString();
}
So, if you will pass a Date and try to print it this will be printed out all the time.
Code:
Date date = new Date();
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(date));
Date : Fri Apr 29 04:53:16 GMT 2016
Sample Output : 2016-04-29
Imports required :
import java.util.Date; //for new Date()
import java.text.SimpleDateFormat; // for the format change
System.out.println("pickUpDate " + sdf.format(pickUpDate));
You can use the above code to get formatted Date as String
Use this Code:
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date pickUpDate =sdf.parse("2007-06-21");
System.out.println("pickUpDate "+sdf.format(pickUpDate));
Hope it'll help you.
String your_format_date=sdf.format(pickUpDate);
System.out.println("pick Up Date " + your_format_date);
Date isn't a date. It's a timestamp. That's some impressive API design, isn't it?
The type you need is now java.time.LocalDate, added in Java 8.
If you can't use Java 8, you can use ThreeTen, a backport for Java 7.

How do I create the java date object without time stamp [duplicate]

This question already has answers here:
Java program to get the current date without timestamp
(17 answers)
Closed 6 years ago.
I want to create a Date object without a TimeZone (eg : 2007-06-21). Is this possible?
When I use the following method it prints like Thu Jun 21 00:00:00 GMT 2007
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
TimeZone timeZone = TimeZone.getTimeZone("GMT");
timeZone.setDefault(timeZone);
sdf.setTimeZone(timeZone);
Date pickUpDate = sdf.parse("2007-06-21");
System.out.println(pickUpDate);
If you want to format a date, you need to use DateFormat or something similar. A Date is just an instant in time - the number of milliseconds since the Unix epoch. It doesn't have any idea of time zone, calendar system or format. The toString() method always uses the system local time zone, and always formats it in a default way. From the documentation:
Converts this Date object to a String of the form:
dow mon dd hh:mm:ss zzz yyyy
So it's behaving exactly as documented.
You've already got a DateFormat with the right format, so you just need to call format on it:
System.out.println("pickUpDate" + sdf.format(pickUpDate));
Of course it doesn't make much sense in your sample, given that you've only just parsed it - but presumably you'd normally be passing the date around first.
Note that if this is for interaction with a database, it would be better not to pass it as a string at all. Keep the value in a "native" representation for as much of the time as possible, and use something like PreparedStatement.setDate to pass it to the database.
As an aside, if you can possibly change to use Joda Time or the new date/time API in Java 8 (java.time.*) you'll have a much smoother time of it with anything date/time-related. The Date/Calendar API is truly dreadful.
This is the toString() of the java.util.Date
public String toString() {
// "EEE MMM dd HH:mm:ss zzz yyyy";
BaseCalendar.Date date = normalize();
StringBuilder sb = new StringBuilder(28);
int index = date.getDayOfWeek();
if (index == gcal.SUNDAY) {
index = 8;
}
convertToAbbr(sb, wtb[index]).append(' '); // EEE
convertToAbbr(sb, wtb[date.getMonth() - 1 + 2 + 7]).append(' '); // MMM
CalendarUtils.sprintf0d(sb, date.getDayOfMonth(), 2).append(' '); // dd
CalendarUtils.sprintf0d(sb, date.getHours(), 2).append(':'); // HH
CalendarUtils.sprintf0d(sb, date.getMinutes(), 2).append(':'); // mm
CalendarUtils.sprintf0d(sb, date.getSeconds(), 2).append(' '); // ss
TimeZone zi = date.getZone();
if (zi != null) {
sb.append(zi.getDisplayName(date.isDaylightTime(), zi.SHORT, Locale.US)); // zzz
} else {
sb.append("GMT");
}
sb.append(' ').append(date.getYear()); // yyyy
return sb.toString();
}
So, if you will pass a Date and try to print it this will be printed out all the time.
Code:
Date date = new Date();
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(date));
Date : Fri Apr 29 04:53:16 GMT 2016
Sample Output : 2016-04-29
Imports required :
import java.util.Date; //for new Date()
import java.text.SimpleDateFormat; // for the format change
System.out.println("pickUpDate " + sdf.format(pickUpDate));
You can use the above code to get formatted Date as String
Use this Code:
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date pickUpDate =sdf.parse("2007-06-21");
System.out.println("pickUpDate "+sdf.format(pickUpDate));
Hope it'll help you.
String your_format_date=sdf.format(pickUpDate);
System.out.println("pick Up Date " + your_format_date);
Date isn't a date. It's a timestamp. That's some impressive API design, isn't it?
The type you need is now java.time.LocalDate, added in Java 8.
If you can't use Java 8, you can use ThreeTen, a backport for Java 7.

Can SimpleDateFormat parse floating point seconds? [duplicate]

Background:
In my database table, I have two timestamps
timeStamp1 = 2011-08-23 14:57:26.662
timeStamp2 = 2011-08-23 14:57:26.9
When I do an "ORDER BY TIMESTAMP ASC", timeStamp2 is considered as the greater timestamp(which is correct).
Requirement: I need to get the difference of these timestamps (timeStamp2 - timeStamp1)
My implementation:
public static String timeDifference(String now, String prev) {
try {
final Date currentParsed = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse(now);
final Date previousParsed = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse(prev);
long difference = currentParsed.getTime() - previousParsed.getTime();
return "" + difference;
} catch (ParseException e) {
return "Unknown";
}
}
The answer should have been 238ms, but the value that is returned is -653ms.
I'm not sure what I'm doing wrong. Any suggestions?
The format you are parsing and the format uses doesn't match. You expect a three digit field and are only providing one digits. It takes 9 and assumes you mean 009 when what you want is 900. Date formats are complicated and when you prove dates in a different format it may parse them differently to you.
The documentation says S means the number of milli-seconds and the number in that field is 9, so it is behaving correctly.
EDIT: This example may help
final SimpleDateFormat ss_SSS = new SimpleDateFormat("ss.SSS");
ss_SSS.setTimeZone(TimeZone.getTimeZone("GMT"));
for (String text : "0.9, 0.456, 0.123456".split(", ")) {
System.out.println(text + " parsed as \"ss.SSS\" is "
+ ss_SSS.parse(text).getTime() + " millis");
}
prints
0.9 parsed as "ss.SSS" is 9 millis
0.456 parsed as "ss.SSS" is 456 millis
0.123456 parsed as "ss.SSS" is 123456 millis
I'm not entirely sure, but the JavaDoc states this:
For parsing, the number of pattern letters is ignored unless it's needed to separate two adjacent fields.
This indicates that the milliseconds from 2011-08-23 14:57:26.9 would be parsed as 9 instead of 900. Adding the trailing zeros might work: 2011-08-23 14:57:26.900.
I'd suggest using Joda-Time. It handles these situations properly. In the following example, the milliseconds are correctly parsed as 200ms.
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public class ParseMillis {
public static void main(String[] args) {
String s = "00:00:01.2";
DateTimeFormatter format = DateTimeFormat.forPattern("HH:mm:ss.S");
DateTime dateTime = format.parseDateTime(s);
System.out.println(dateTime.getMillisOfSecond());
}
}
I had the same problem with too accurate time from my logfiles with 6 digit milliseconds. Parsing Time gave up to 16 minutes difference! WTF?
16-JAN-12 04.00.00.999999 PM GMT --> 16 Jan 2012 04:16:39 GMT
Changing the number of digits reduced the erroneous difference and thanks to this thread I could identify the problem:
16-JAN-12 04.00.00.99999 PM GMT --> 16 Jan 2012 04:01:39 GMT
16-JAN-12 04.00.00.9999 PM GMT --> 16 Jan 2012 04:00:09 GMT
16-JAN-12 04.00.00.999 PM GMT --> 16 Jan 2012 04:00:00 GMT
As SimpleDateFormat internally handles only 3 digits I removed the unnecessary with a small regex (ignoring round-off errors, working for 1 up to n digits):
str = str.replaceAll("(\\.[0-9]{3})[0-9]*( [AP]M)", "$1$2");
Thanks to #Peter Lawrey for your answer, prevented me going insane :-)

How to print time up to 6 digits of precision for seconds value

I have a value in column which is of type timestamp. Lets say I have a value 2007-05-04 08:48:40.969774
Now, when trying to fetch the value from the database and return this timestamp value to a function, what SimpleDateFormatter pattern should I use so that the fraction part beside the seconds also gets returned.
I have used yyyy-MM-dd hh:mm:ss, but that only returns till the seconds and ignores the fraction present beside the seconds(.969774). I need help in returning this fraction part also, with a 6-digits precision.
The default ways of formatting a java.util.Date (or java.sql.Timestamp) has only millisecond precision. You can use yyyy-MM-dd hh:mm:ss.SSS to get that millisecond precision.
A java.sql.Timestamp actually does have (up to) nanosecond precision (assuming the database server and the driver actually support it). The easiest way to format it in Java 8 is to convert the timestamp to a java.time.LocalDateTime (using Timestamp.toLocalDateTime()) and use the java.time formatting options in java.time.format.DateTimeFormatter which support up to nanoseconds.
If you use Java 7 or earlier it will take some extra effort, as the normal date formatters don't support it. For example you could use a dateformatter with pattern yyyy-MM-dd hh:mm:ss (to only format up to seconds) and append the sub-second nano seconds (with appropriate zero-padding) of Timestamp.getNanos() yourself.
You have to have a way of obtaining a micro-second timestamp. I use System.currentTimeMillis() with System.nanoTime() in combination. Then you need a way to display it. You can divide it by 1000 and display milliseconds as normal, then display the last 3 digits of the time. i.e. Have a time which is like
long timeUS = System.currentTimeMillis() * 1000 + micros;
here is a more detailed example
HiresTimer.java and HiresTimerTest.java
The test prints
2012/04/09T14:22:13.656008
2012/04/09T14:22:13.656840
2012/04/09T14:22:13.656958
2012/04/09T14:22:13.657066
....
2012/04/09T14:22:13.665249
2012/04/09T14:22:13.665392
2012/04/09T14:22:13.665473
2012/04/09T14:22:13.665581
EDIT: The relevant code is
private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy/MM/dd'T'HH:mm:ss.SSS");
private static final DecimalFormat DF = new DecimalFormat("000");
public static String toString(long timeUS) {
return SDF.format(timeUS / 1000) + DF.format(timeUS % 1000);
}
In Java 8 and later, the java.time package has support for parsing and manipulating date/times to nanosecond precision. That means up to 9 digits in the fraction of a second.
Dealing with micro- or nano-seconds in Java is not always straightforward.
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
public class Main {
public static void main(String args[]) throws Exception {
long time = System.currentTimeMillis();
Date d = new Date(time);
Timestamp t = new Timestamp(time);
/* micro-seconds as OP requested */
{
t.setNanos(123456000);
System.out.println(d);
System.out.println(t);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss'.'");
NumberFormat nf = new DecimalFormat("000000");
System.out.println(df.format(t.getTime()) + nf.format(t.getNanos() / 1000));
/* using Java Time API (available since JDK 1.8) */
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS");
LocalDateTime ldt = t.toLocalDateTime();
System.out.println(ldt.format(dtf));
}
/* nanoseconds just for the sake of completness */
{
t.setNanos(123456789);
System.out.println(t);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss'.'");
NumberFormat nf = new DecimalFormat("000000000");
System.out.println(df.format(t.getTime()) + nf.format(t.getNanos()));
/* using Java Time API (available since JDK 1.8) */
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.nnnnnnnnn");
LocalDateTime ldt = t.toLocalDateTime();
System.out.println(ldt.format(dtf));
}
}
}
The output produced is (in my country, my locale):
Tue Aug 31 13:26:08 CEST 2021
2021-08-31 13:26:08.123456
2021-08-31 13:26:08.123456
2021-08-31 13:26:08.123456
2021-08-31 13:26:08.123456789
2021-08-31 13:26:08.123456789
2021-08-31 13:26:08.123456789

Categories

Resources