How to serialize Date and DateTime with URQL? - graphql

I am using urql-react as my React client in a project. I need to make sure that any date variables in query/mutation with Date scalar type are serialized into YYYY-DD-MM format while for DateTime scalar type, the date object is serialized in YYYY-DD-MMTHH:mm:ssZ format.
How can this be achieved on a central level?

Related

Why Date field is coming with Character T in JPA

Hi I am trying to retrieve and Save Date from Oracle Table using JPA . The column type is DATE in Oracle.
In JPA entity i configured as
#Column(name = "NOTE_DATE")
private Date noteDate;
In Json why i am getting the date with a T character . Eventhough its not saving that (T)character ins DB why its asking for date as
Successful Save
"noteDate": "2021-11-01T20:00:05"
Failure Save
"noteDate": "2021-11-01 20:00:05"
The 2021-11-01T20:00:05 date representation is generated by your JSON serializer which converts your Date to String and not by JPA. By default, Spring uses Jackson as JSON serializer which converts Date to its ISO 8601 representation.
For your error when you try to save your object using
"noteDate": "2021-11-01 20:00:05"
For me, it is a deserialization problem: Jackson can not convert your JSON to your Java object.

Convert TimeStamp To Joda DateTime Without Any Timezone Conversion

I have timestamp value in Oracle database column stored in UTC. I want to read it through spring's jdbcTemplate and convert it to joda DateTime object without any timezone conversion i.e. read it as is without converting or losing timezone.
For e.g. if the input timestamp is 2019-03-08 15:07:37.232, I would like to have the DateTime object with the value 2019-03-08T15:07:37.232Z
How can I achieve this?
Note this code - new DateTime(timestamp.getTime(), DateTimeZone.UTC)) does not help since it assumes that the input timestamp is in local timezone and reconverts it to UTC. For the above input is 2019-03-08 15:07:37.232 the outcome comes to 2019-03-08T09:37:37.232Z
Thanks.
One solution that worked was to change the way the timestamp column value is retrieved from the database. The below snippet works for converting a timestamp column value to joda DateTime without losing/converting the timezone
new DateTime(resultSet.getTimestamp("CreatedDateTime",
Calendar.getInstance(TimeZone.getTimeZone("UTC"))), DateTimeZone.UTC)
Hope that helps others too.
If you already have a DateTime object there is method
public DateTime withZone(DateTimeZone newZone)
which would return a copy of datetime with a different time zone, preserving the millisecond instant
eg:- dateTime.withZone(DateTimeZone.UTC)

Can't remove hours and minutes from datatable

I'm trying to get release date field with y-m-d format.. Actually time format (for y-m-d) seems fine but also it gives h:m:s too, I changed time format at server side and removed h:m:s but datatable still shows them
What I get
2012-04-11 00:00:00
What I want
2012-04-11
Released_at field (Metronic theme - json datatable)
{
field: "released_at",
title: "Release Date",
type: "date",
format: "YYYY/MM/DD"
}
time format (I'm using laravel framework)
protected $dateFormat = "Y-m-d";
How do I fix this ?
Datatable accept column type same as mysql type.
You are trying to convert mysql dateTime column to Date in datatable, which is not possible from datatable.
In your code you have declared
format: "YYYY/MM/DD" // but actual type is dateTime as per mysql.So it will append time next to it.
If you are storing only date in mysql then you can change column type to Date, and your problem gets solved.
And if you want to convert string to date in laravel you can follow this post.
Your database column is of type dateTime which, by definition, includes both date and time information. If you want to remove the time at a database level then use the date type instead
$table->date("released_at")->nullable();
If instead of removing the time from the database, you just want to ignore it for certain parts of your application, you can leverage the fact than in Laravel all dates coming from your models are Carbon\Carbon instances, so you could do
$model->releasedAt->format('y-m-d'); // Returns '18-09-11'

Date format handling in Spring MVC

HI I have a problem in my current Spring +JPA project. My entity object and bean object for web page are same.
From web page using jquery i am reading date in (dd-mon-yyyy) format from screen and saving it to database. The field is of Date type in my bean class.
During update i am fetching values from database and displaying the same in web page. But this time the date format has chnaged to different fromat(yyyy/mm/dd) on screen.
So while saving again i am getting error, as the date format has been changed and i am unable to parse the value received form screen.
So is there any proper way to handle this situation.
I assume that you are storing the dates in database as a DATE or DATETIME type. If so, the value should be a Date object in Java. In order to output the date value as dd-mon-yyyy, you will need to use SimpleDateFormat to convert the date object into a string representation in your desired format:
new SimpleDateFormat("dd-MMM-yyyy").format(date);
An additional note: for an internal-only API, it doesn't matter what format you choose as long as you are consistent everywhere. But if you are creating something that could one day be exposed publicly, I would suggest that you send/receive date and time value in the ISO8601 format: yyyy-MM-ddTHH:mm:ssZ. This format is understood by everyone everywhere.
Update:
Based on your comment, I would say add a new method to your bean class:
private DateFormat dateformat = new SimpleDateFormat("dd-MMM-yyyy");
public String getFromDateString() {
return dateformat.format(fromDate);
}
And then in your JSP, call ${bean.fromDateString}.

How to convert a cassandra date object into epoch timestamp in java

I am creating a java plugin for moving data from cassandra database to elastic search. I am getting all the data but the date which I am getting from the database is in human readable form ie Row[Fri Jul 25 11:36:10 IST 2014].I want this to be converted to epoch timestamp format like 1414386721.
I do not know Cassandra DB, but according to this doc your driver should be translating the date-time value in Cassandra to a java.util.Date object in Java.
You may be confused about how a java.util.Date object works. The j.u.Date class is confusing and difficult in many ways, one of which is that while a Date has no time zone its toString implementation on-the-fly applies the JVM’s current default time zone as it generates the string.
You may also be new to date-time work and therefore confusing a date-time object with its String representation. Consider that 1.4 is a number and should not be confused with its representation as a String in the format of a price €1.40. Likewise a date-time object is not a String but can be represented as a String generated any number of formats.
Lastly, if you are indeed getting a java.util.Date object, learn to convert that to either the Joda-Time library or the java.time library. The java.util.Date and .Calendar classes are notoriously troublesome.

Resources