i am using datepicker inside my project and storing the date and time inside sql database. so my problem is that when i chose time: 12:13 and store it inside the database, i try to retrieve this time and i get 00:13 instead of 12:13.
Only 12:00 ose not work
This is my format that I use to store:
SimpleDateFormat test = new SimpleDateFormat("yyyy-MM-d HH:mm:ss");
And this is raw from my sql create table :
work_time datetime NOT NULL
As you see this is datatime type, so any suggestion how to fix this?
Humm... It seems that 00:00 means 12:00 in french time. This isn't a bug or problem, its the way its set.
Related
I am trying to fill 2 date objects, one in Local time and the other in UTC.
I AM NOT TRYING TO PRINT THE DATE AS A STRING IN GMT/UTC, please do not suggest DateFormatting, and dont say its a duplicate until you read the full question.
Local, I have no problem:
Date dateLocal = new Date();
The problem is I cant get the utcDate to be UTC.
Using a Calendar like so:
TimeZone utcTimeZone = TimeZone.getTimeZone("UTC");
Calendar c = new GregorianCalendar();
c.setTime(new Date());
c.setTimeZone(TimeZone.getTimeZone(utcTimeZone.getID()));
Date utcDate = c.getTime();
When debugged or submitted to the webservice, utcDate shows in my local timezone, instead of UTC.
Using Joda:
DateTime utcDateTime = DateTime.now(DateTimeZone.UTC);
Date utcDate = utcDateTime.toDate();
Same issue, utcDate when debugged/submitted to webservice is showing in local time.
Here is how the object looks when debugged:
This is an issue because this causes the webservice (which i have no access to) to think this time is UTC, so when it does its work and conversions, the time is always off by 4 hours, since for me the UTC to Local conversion is GMT -4.
The ONLY way i have been able to get this to submit the date in UTC time is by adding:
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
BUT this also changes the LocalTime object, even though this object was defined and set before the default TimeZone was changed.
So i get it, the Date() object uses the JVM locale, so any time a Date is created, its created in the default timezone, and apparently whenever the default timezone is changed, all of the Date objects (even if they are already created) change to the new default timezone... I know Date objects are just the millis between now and 1970 whatever, but the TimeZone is obviously being taken into account in the Webservice and this is messing up my results...how can i get the dates the way i want?
I want to get the current date as yyyy-MM-dd from SQLite, I use the following query:
***SELECT date('now')***
But instead of returning the current date, it returns the next day from today.
For example, today (2015-12-01) I run the query and it returns (2015-12-02).
What I did wrong?
Image running query + calendar
sqlite date and time functions use UTC time zone internally. In UTC the date was already 2015-12-02.
If you want to use another timezone, you need to specify it explicitly, e.g.
select date('now','-05:00');
I'd suggest to use UTC millisecond timestamps in your database layer though and have the presentation logic such as date formatting with timezone adjustment in your app code.
Reference: https://www.sqlite.org/lang_datefunc.html
I as having time in hh:mm aa format ex: 01:30 AM.
While creating table I am specifying the datatype as TIME. But there is no way of setting its format. By default it is HH:MM:SS format. So while entering AM/PM values it is not added in database.
Is there a way to store my time in my required format using TIME datatype column so that sorting can be done easier.
Also I tried saving the time as TEXT datatype. I order it by taking substirng of AM/PM and do string sorting. The problem in it is 12:30 PM will be not correctly ordered.
ex: 12.30 PM, 12.30 AM, 02.30 PM will be ordered as 12.30 AM,02.30 PM,12.30 PM
How can I order this column and what datatype can I give for this?
Use hh:mm:ss for storing values in the database.
(It's required for correct sorting, and by all built-in time functions.)
Use the hh:mm aa format (or any other format) for displaying values.
You cannot forsee what format is the default for your user's locale, or if the user has selected another time format, so you must be prepared to format times for displaying them in any case.
I'm saving the timestamp of a user action to a SQLite Database. It all works fine except the time stamp is in GMT. I need to change it to +x hours ahead, but I'm having trouble formatting the SQL command:
myDB.execSQL("INSERT INTO "+ DATE_TABLE+" VALUES(null,'"+spt.toString()+"',datetime())" );
datetime() function gives the GMT time/date stamp. AS shown here http://www.sqlite.org/lang_datefunc.html
I tried datetime('localtime') which gives a value of NULL.
I tried datetime('+x hour') with hour and hours which returned NULL.
I want to know how to alter the time stamp for localtime?
Try these:
datetime('now','localtime')
datetime('now','+4 hours')
The System-Time of my Android emulator is correct (currently 13:42). But when i use the datetime('now')-function to set the current time in my SQLite Database, the returned value is wrong (11:42).
Is there another time i need to set to get this working correctly?
The return value of datetime('now') is in UTC.
Try
datetime('now', 'localtime')
datetime('now') will return in GMT UTC - which you probably should do then handle the conversion to your local timezone in the app. If you keep it in UTC in the database, then convert it in your activities, your app will work correctly as the user moves around timezones