why get the wrong week display name from unix timestamp in Java - java

Now I am using this Java code to get weeks display name:
String dateTime = CustomDatetimeUtil.timeStamp2Date(response.getStatisticTime(), "yyyy-MM-dd");
response.setStatisticDate(dateTime);
String displayName = Instant.ofEpochSecond(response.getStatisticTime())
.atZone(ZoneId.of("Asia/Shanghai"))
.getDayOfWeek()
.getDisplayName(TextStyle.FULL, Locale.CHINA);
the response.getStatisticTime() value is 1614783599999. But I get the display name is · Sunday(星期日). the correct value is wednesday. what should I do to get the right value?

You're using Instant.ofEpochSecond- but the value you've got is large enough that it's presumably the number of milliseconds since the Unix epoch, not the number of seconds.
So basically you need to use Instant.ofEpochMilli instead.
I would recommend using the Epoch Converter site in future too - that allows you to put in a numeric value, and it will show you what that value means including the units it has assumed when performing the conversion.

Related

What format is this date? [17/6/2015 5:50:22 5 -120]

Context: Getting date from a third party data source as below. I am using Pig script to transform this string to date.
Script:
a= LOAD '/user/hit_data.tsv' using PigStorage('\t');
b= FOREACH a GENERATE $0 as post_t_time_info;
c= FOREACH b GENERATE ToDate(post_t_time_info,'DD/MM/YYYY HH:mm:ss e ZZZ')
Sample value the date object takes:
17/6/2015 5:50:22 5 -120
17/6/2015 0:7:6 5 240
I am unable to understand what is -120/240. I tried with timezone(ZZZ) and milliseconds (SSS) but appears to be incorrect.
My current format used is 'DD/MM/YYYY HH:mm:ss e X', where X is unknown and looking forward for appropriate pattern for it.
Thanks!
Reference:
http://userguide.icu-project.org/formatparse/datetime
http://www.unicode.org/reports/tr35/tr35-25.html#Time_Zone_Fallback
Chances are that -120 and 240 are indeed time zone offsets. They are likely in terms of minutes, not hours. However, there's no standard for that, so it could be minutes east of GMT, or minutes west of GMT.
In other words, -120 could be UTC+02:00, or UTC-02:00. 240 could be UTC+04:00, or UTC-04:00.
For example, if it was obtained from the JavaScript Date object's getTimezoneOffset function, the sign will be opposite of what you might expect. It would have positive values to the west, while the usual ISO8601 convention has positive values to the east.
Since you are the one obtaining the data, you are in a much better position than us to identify the source and disambiguate. If it's from a third-party, look in their specs, or contact them and ask.
Also - You said you were using Apache Pig, but according to their documentation, the ToDate function uses Java's SimpleDateFormat - which does not use the same format qualifiers as ICU, nor does it have a format qualifier that recognizes time zone offsets in terms of minutes. You will likely need to write your own function instead of using just the built-in ToDate.

Java : Custom Timestamp Format : Verify value range to minute precision

My Objective is
to create a java class that can handle the below two requirements
(A) 1. Verify if the format of a timestamp matches with expected format.
CCYY-MM-DD'T'hh:mm:ss'.0000000000+'uh:um"
Ex: the expected format
is not static. It may be either of these
"2013-09-10T18:30:20.123456+10:00" or
"2013-09-10T18:30:20.123+10:00".
I am not bothered about the
precision and value. Only the format matters.
(B) 2. Verify if the timestamp is in a certain range.
Ex: Verify if the timestamp is in between "2013-09-10 18:27" and "2013-09-10 18:33". (verification is only upto minute level precision) (may be a delta of + or - 2min)
The question on first objective was posted here
Java : Custom Timestamp Format : verify format to microsec precision
In refrence to the second objective :
The Question :
How to verify if the timestamp is within the given range to minute precision using Java class?
The inputs to the class will be three arguments
The timestamp to be verfied as a string
The lower range timestamp as string
the higer range timestamp as string.
I tried this
String date = "2013-09-10T18:30:12.123456789+10:00"
SimpleDateFormat df2 = new SimpleDateFormat("yyyyMMddHHmm");
date = date.substring(0,22)+"+10:00";
Date newDate = df.parse(date);
timestamp_value = df2.format(newDate);
timestamp_value is now 201309101830
Check if timestamp_value is greater than 201309101827.
Check if timestamp_value is lesser_ than 201309101833
This is okay.
However, i feel this i a crude way of achieving it, so I would like to know if there is any better solution.
If i could convert to Timestamp object, i think .before and .after can help. Any suggestions ?
1) "2013-09-10T18:30:20.123+10:00" is XSD dateTime format and the best way to parse it is this
XMLGregorianCalendar xgc = javax.xml.datatype.DatatypeFactory.newInstance().newXMLGregorianCalendar(date);
if date format is wrong IllegalArgumentException will be thrown
2) to verify if a timestamp is within a certain range the easiest way is to get millis from dates, truncate to minutes and use arithmetic comparison t >= from - delta && t <= to + delta

How do I store time in a variable?

One of my classes Event will have an instance field which is called timeStamp. Now, I have another class which will set the timeStamp according to some other algorithm which is not really relevant here.
My question is what type should I store this timeStamp in? From what I've researched so far I have the impression that it should be calculated in milliseconds and thus store it in a double perhaps.
Basically the Clock class I have simulates time in the following format : hh:mm:ss. However, since it's a discrete event simulation that I'm developing it jumps from event to event, which it determines by timeStamp value i.e. each event object has a timeStamp value which is stored in a PrioityQueue. So I thought about storing the timeStamp in the same format as the Clock , which I guess would involve me creating a new class TimeStamp that then becomes the type of the timestamp. Or should I just make the clock simulate time in milliseconds?
What are your thoughts on this? I'm not sure on the most efficient/clean way to implement this.
When a date is stored as milliseconds since the epoch, you should use a long.
There's no need for a double, since you're not interested in fractions of a millisecond.
You can't use an int because the maximum int value is only large enough to represent approximately one month in millis.
You can get such a value like this:
long millisSinceEpoch = Calendar.getInstance().getTimeInMillis();
Store the milliseconds in a long.
You can use the DateTime class in Joda Time to perform all sorts of intricacies on the resulting number. This overload allows you to plug the milliseconds value directly into a DateTime object.

How to get date/time with millisecond from oracle database using java?

I have the following TIMESTAMP value in oracle database.
26-JUL-12 11.01.40.000000000 AM
When i getting this value from table , it displays without millisecond value like,
26-JUL-12 11.01.40 AM
But, i want to get this value like this format,
26-JUL-12 11.01.40.000 AM
Is there any possible to get timestamp value in this format?
Please help me.
Thanks in advance..
select to_char(systimestamp,'dd-mon-rrrr hh:mi:ss:ff3am') from dual;
This article states:
An Oracle DATE stores the date and time to the second. An Oracle TIMESTAMP stores the date and time to up to 9 digits of subsecond precision, depending on the available hardware.
Both are implemented by storing the various components of the date and the time in a packed binary format. From the Oracle Concepts Guide section on dates
Oracle uses its own internal format to store dates. Date data is stored in fixed-length fields of seven bytes each, corresponding to century, year, month, day, hour, minute, and second.
You can use the DUMP() function to see the internal representation of any particular date (or any other value for that matter), but that's probably more than you need (or want) to know.

Java microseconds formatter

In database, i have timestamp values like
2005-JAN-13 07:15:31.22222
I want to format above value in java to
2005-05-13 07:15:31.22222
fot this i used formetter: yyyy-MM-dd HH:mm:ss:SSSSS
but when i use above formetter , it is giving value as follows:
2005-05-13 07:15:31:00222
instead of 2005-05-13 07:15:31.22222
can any one pls suggest java formater to get the value as follows:
2005-05-13 07:15:31.22222
AFAIK Java dates are up to millisecond precision, thus you can't format microseconds. The best you can get would be 2005-05-13 07:15:31.22200 (note that the trailing zeros would have to be appended by you, since the millisecond part would be 222 and thus would be formatted as 00222 when having the format string like SSSSS).
the standard date formats do not support microsecond precision. What you are getting is the milliseconds formatted into a 5 character wide millisecond field.
You would need to do your own manual formatting on microseconds and append it to the string yourself.

Categories