Date stored in different format - date

I have this date "2018-05-30T16:19:58.016Z" coming from my Angular app.
In Spring, the field date is as follows :
#JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
private Date date;
The date is well stored, but with this format YYYY-MM-dd.
Is there anything that I'm missing ?

MySql date type can't hold data with timestamp. It has to be datetime in order to contain date time with timestamp data.

You probably have to specify the date format going out to the storage, as #JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") only specifies the date format for parsing the date into the Date object. Even if your Date has all of the seconds and timezone information, the default Date toString() is still
Formats a date in the date escape format yyyy-mm-dd.
according to the Java 8 docs. So if you are using that, it would most likely drop all that extra information on conversion.
You can look at this Convert java.util.Date to String for information on how to get a Date to a formatted String.

Related

Date formatting in postgresql from existing data

I've date in dd-mm-yyyy format, I need the date in yyyy-mm(month) format. I'm using postgresql.
Thanks in advance.
date values don't have any format. Any formatting you see is applied by your SQL client. To turn a date into a string with a specific format, you can use to_char()
to_char(the_column, 'yyyy-mm')

Reading weird date format in hive

I have a column which contians date as string but in many formats like - dd/MM/yy, dd/MMM/yyy .. etc etc. And I am using the following code to convert all strings to one specific date format (yyyy-MM-dd) in hive :
select
from_unixtime(unix_timestamp('31/02/2021','dd/MM/yyyy'),'yyyy-MM-dd')
but this gives me 2021-03-03 in HIVE.
Is there any other way to identify such invalid dates and give null.
Assume, you recognized format correctly and it is exactly 'dd/MM/yyyy' and date is invalid one '31/02/2021'.
unix_timestamp function in such case will move date to the next month and there is no way to change it's behavior. But you can check if the date double-converted from original string to timestamp and back to original format is the same. In case it is not the same, then the date is invalid one.
case
-- check double-converted date is the same as original string
when from_unixtime(unix_timestamp(date_col,'dd/MM/yyyy'),'dd/MM/yyyy') = date_col
--convert to yyyy-MM-dd if the date is valid
then from_unixtime(unix_timestamp('31/02/2021','dd/MM/yyyy'),'yyyy-MM-dd')
else null -- null if invalid date
end as date_converted

Google App Script - how to set date with timezone in AdminReports.UserUsageReport.get()

I'm using Apps Script and trying to extract data from AdminReports.UserUsageReport.get('all', 'the date') (to get classroom last interaction timestamps), but it always comes with records from another day. For example, if I extract from the 5th, the report brings the 6th together, until 7 o'clock in the morning.
The date parameter is a string and in the documentation says the following:
Represents the date the usage occurred. The timestamp is in the ISO
8601 format, yyyy-mm-dd. We recommend you use your account's time zone
for this.
But if the parameter is a string in yyyy-mm-dd format, how am I going to pass the date with the right time zone?
This code not work:
var myDate = new Date(2021,5,5);
var timezone = Session.getScriptTimeZone();
var date = Utilities.formatDate(myDate, timezone, 'yyyy-MM-dd');
var page = AdminReports.UserUsageReport.get('all', date);
How do I use the date correctly?
Thanks a lot.
If the report dates are UTC, then each may first be converted to JavaScript Date objects using
var myDate = new Date('report_date_string');
The second two lines of your code look like they should follow correctly in converting and formatting the Dates to strings in your local time zone.

BCB6 How to display long date from DataSet Field in label?

I have a problem with displaying the correct date from a TDataSet.
In a TDataSet, I have a date in 'YYYY-MM-DD' format. On the computer, I have set the date in the d.mm.yy format, and so it has to stay, I can not change it.
Now, I have to take the date in the format 'YYYY-MM-DD' from the TDataSet and display it in a TLabel component in this format, but it always shows me the date in the 'YY-MM-DD' format, and that if the date is before 1969 then it adds 100 years.
I have my date in:
MyData->DataSet->Fields->FieldByName("date")
In my DB, my date is in YYYY-MM-YY format.
On my machine, I have the short date set to the 'DD-MM-YY' format, but I need to display my date in the label in 'YYYY-MM-DD' format.
I have no ideas for how to handle this.
TDateTime has a method named FormatString(). You can do this:
MyData->DataSet->Fields->FieldByName(L"date")->AsDateTime.FormatString("yyyy-mm-dd");

Date Column Split in Talend

So I have one big file (13 million rows) and date formatted as:
2009-04-08T01:57:47Z. Now I would like to split it into 2 columns now,
one with just date as dd-MM-yyyy and other with time only hh:MM.
How do I do it?
You can simply use tMap and parseDate/formatDate to do what you want. It is neither necessary nor recommended to implement your own date parsing logic with regexes.
First of all, parse the timestamp using the format yyyy-MM-dd'T'HH:mm:ss'Z'. Then you can use the parsed Date to output the formatted date and time information you want:
dd-MM-yyyy for the date
HH:mm for the time (Note: you mixed up the case in your question, MM stands for the month)
If you put that logic into a tMap:
you will get the following:
Input:
timestamp 2009-04-08T01:57:47Z
Output:
date 08-04-2009
time 01:57
NOTE
Note that when you parse the timestamp with the mentioned format string (yyyy-MM-dd'T'HH:mm:ss'Z'), the time zone information is not parsed (having 'Z' as a literal). Since many applications do not properly set the time zone information anyway but always use 'Z' instead, so this can be safely ignored in most cases.
If you need proper time zone handling and by any chance are able to use Java 7, you may use yyyy-MM-dd'T'HH:mm:ssXXX instead to parse your timestamp.
I'm guessing Talend is falling over on the T and Z part of your date time stamp but this is easily resolved.
As your date time stamp is in a regular pattern we can easily extract the date and time from it with a tExtractRegexFields component.
You'll want to use "^([0-9]{4}-[0-9]{2}-[0-9]{2})T([0-9]{2}:[0-9]{2}):[0-9]{2}Z" as your regex which will capture the date in yyyy-MM-dd format and the time as mm:HH (you'll want to replace the date time field with a date field and a time field in the schema).
Then to format your date to your required format you'll want to use a tMap and use TalendDate.formatDate("dd-MM-yyyy",TalendDate.parseDate("yyyy-MM-dd",row7.date)) to return a string in the dd-MM-yyyy format.