How to get only year from a date stored in OrientDB - java

I'm working on JBake, and would like to get the year part of the date at which a document was stored.
I know I can get the date by doing
SELECT date FROM post
And OrientDB will return me a java.util.Date.
I know I can also add the year as another field, but I find this data duplication unsatistfying.
So, is it possible to store the date, and to to have the year extracted through some kind of SQL "function" ?
Typically I would love to do
SELECT date, {date.getYear()} FROM post

You could use the format() function using the SimpleDateFormat syntax:
select date.format('YYYY') from post

Related

Stamp format in Mongo db/Robo 3t?

I have certain object that has a date property. When i print the date value in Java i get a normal date like "11/08/2021". The thing is, when i try to check the date of my object (which is stored in mongoDB) in Robo 3T, i get a number like that : 1507037760657.
I searched and i found this kind of converter that allows to convert timeStamp format to date format. But that value : 1507037760657 is converted to some really weird date like 15/2/49726.
Is mongoDB using another timestamp format i don't know? How can i get a good date from this number ? 1507037760657
Thanks

Set date format dd/mm/yyyy in SQL table

I want to save the dates in my database in the dd/mm/yyyy format, because I want to import the table that contains them in a JTable (Java SE) and I want to display them in this format. Is it possible to directly save the date on my database in this format or I must do it in another way? My DB is write in SQL and I use MySQL.
Date are dates. It doesn't exists a format for dates.
What you can obtain is a string with a particular format from the date.
Note that the format probably is not dd/mm/yyyy but dd/MM/yyyy because mm is for minutes, not for months.
So basically you have two possibilities:
Save dates as Date and retrieve them as string with the requested format
Convert dates to strings and save them as formatted strings (VARCHAR for example)
To convert a Date to a String in MySql you can use the function DATE_FORMAT
If you like to convert them in java you can use a SimpleDateFormat
There is no possibility to save the date in the specified format but yes you can set the type of that field as String (in MySql varchar) and you can save whatever you want.
You can't do it,
or else you have to use VARCHAR or CHAR, but thats not recommended.
save the date in DATE datatype with format yyyy-mm-dd.
don't mess with it.
When you fetch the records, use DATE_FORMAT function to convert it into your format. (if you use MySQL)
like
SELECT DATE_FORMAT(CURDATE(), '%d-%m-%Y');
in your case
SELECT DATE_FORMAT(< your_date_field >, '%d-%m-%Y');
Use DATE_FORMAT function to change the format of a date in MySQL.
SELECT DATE_FORMAT(CURDATE(),'%d/%m/%Y')
SELECT DATE_FORMAT(column_name,'%d/%m/%Y') # FROM tablename
Refer to documentation for more details.

Store date as yyyy-MM-dd in database without time? [duplicate]

This question already has answers here:
Java Date - Insert into database
(10 answers)
Closed 6 years ago.
How can I best store a String with format yyyy-MM-dd (without time declaration) in SQL database (postgres)?
I later want to use that String always as Date type. I also want to execute query against the database to give me records that are before or after that Date.
Should I store it as a String or as a Date type in DB?
If I store it as a Date, in database I see yyyy-MM-dd HH:mm:ss. How could I prevent the time declaration?
If you do not want to store a time component, then use the DATE data type. It does not have a time or a time zone component, so is useful for dates of birth, dates of employment start/end, and other data for which the time is not relevant.
http://www.postgresql.org/docs/9.3/static/datatype-datetime.html
The display format is a matter for the application -- just use the correct data type. YYYY-MM-DD is documented as the best format for suplying dates, though.
Always recommended one is Date with time-stamp. If you don't need then while storing store it as 00:00:00.(Use Sql Date for date without time-stamp.)
Use business logical in order to truncate the time and the format you required. Service layer you can play with date and in most of DB its better to store Date with timestamp.

Sqlite Query is not working properly

I have created a database with a String column which contains dates in String format (dd/mm/yyyy). I want to fetch the data from that table which is between two given dates, but when I tried with the below query, I found that it doesn't make any difference what month and year I have selected; it compares the "dd" field only from "dd/mm/yy".
The below query will display all the data which is between day 14 to 25 from every month and year. I want data between the given date, month, and year.
Select * from RunningLog
where CAST(RunDate AS DATETIME) between CAST('14/04/2011' AS DATETIME) and
CAST('25/04/2011' AS DATETIME)
Please see my answer here about how dates are (or are not) stored in sqlite3. Sqlite doesn't have a date field, so its actually stored as a string. Trying to sort / filter on this will prove to be difficult. Instead use an int field, and store the time in milliseconds.
I prefer INTEGER / Unix time storage, then use the built in date and time functions to format when pulling from DB.
Example:
long millis = Calendar.getTimeInMillis();
Store millis in the database as an integer. Then, refer to the first link on how to use the date and time functions in sqlite3.
Sqlite3 documentation does not say it can cast a datetime: http://www.sqlite.org/lang_expr.html (refer to Cast expressions numeral). Why don't you do that from Java? I mean, how are you storing the dates in the database? I highly recommend saving a long (Unix time); that way you can easily get a couple of long numbers that represent an exact date and time and use them to query your table.
Recomendation: Use Datetime fields in the database and JodaTime as a Time library.
Is quite easy to parse the datetime into a DateTime object and then you have many useful methods to compare and work with dates.
Also, your SQL queries will be better.
You can compute the number of seconds between two dates. Here is an example:
SELECT strftime('%s','now') - strftime('%s','2004-01-01 02:34:56');
Based on the sign of the difference you can say if one date is before another. In your case you have to do two comparisons, so you have to verify the sign of two differences. Here you can find other examples, maybe they give you other ideas (http://www.sqlite.org/cvstrac/wiki?p=DateAndTimeFunctions).

how to combine java.util.Calendar and HQL Between

I'm trying to get the books that were loaned between two dates.
Since data has the lovely 2009 is shown as 109 feature I decided to use calendars.
However when writing my HQL I ran into the problem that BETWEEN doesn't view a Calendar as a date.
Now I'm wondering if there's a solution for this.
Or am I stuck writing functions in my class to get the hour, day, month, year and write a long where statement?
query = session.createQuery("from model.Book book where book.loaned between :earliest and :latest");
The problem is that the between only works with a date object. and loaned is Javva.Util.Calendar.
You can get a Date from a Calendar using Calendar.getTime - does that help you? You may run into time zone issues if you're not careful, admittedly... how exactly are the dates stored in the database?

Categories

Resources