In my appliaction I have to store current date into the database. How can i get the current date and is there is any specific format to store date in database.
Better would be to store the date/time in long in Database and then fetch the long date/time from Database and specify the required format using SimpleDateFormat.
SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:
TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
REAL as Julian day numbers, the number of days since noon in
Greenwich on November 24, 4714 B.C. according to the proleptic
Gregorian calendar.
INTEGER as Unix Time, the number of seconds since 1970-01-01
00:00:00 UTC.
Applications can chose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions.
Now for how to insert date in after it.
Use PreparedStatement#setString() or #setLong() respectively.
Hope this explanation works for you..
first convert the date to be stored to a String object using SimpleDateFormat class
code sample:
Date dateToBeStored = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd"); // this format will help you to convert date to 2012/07/04 format string
String dateString = formatter.format(dateToBeStored); // convert string
now read the date string from DB. you should have some means to get the date back from DB.
String readDateStringFromDB = readDate();
Now parse the read date string to date object by parse method of SimpleDateFormat class
Date dateObj = formatter.parse(readDateStringFromDB); // now you have the Date object back
SimpleDateFormat sdfDateTime = new SimpleDateFormat("dd/MM/yyyy"+ "",Locale.US);
String newtime = sdfDateTime.format(new Date(System.currentTimeMillis()));
You can use the Date class to get the epoch time, which could then be stored in the database as an integer. Alternatively you could convert the epoch time to regular time and store it as a date data type.
https://developer.android.com/reference/java/text/SimpleDateFormat
Related
I have an SQLite database in an Android project with a Date column that stores the date-time as String in dd-mm-yyyy HH-mm-ss format. I need to sort it based on the descending order of the date.
Or, convert it to the standard yyyy-MM-dd HH-mm-ss format and then sort it.
The general ORDER BY DATETIME(coulmn_name) doesnt work.
NOTE:
This is not a duplicate question, other answers advice to change the database schema (Which is not possible, because I have data stored already)
I would like to suggest an alternative approach to the one you are taking. I personally ran into the same issue and solved it by not using a string date at all.
Instead i converted the date to epoch milliseconds ie unix timestamp and saved that. Then a sort is a simple order by the timestamp.
You can use the following approach:
SimpleDateFormat sdf = new SimpleDateFormat ("dd-MM-yyyy HH:mm:ss");
Date currentDate = sdf.parse(yourdatestring);
//Get the calendar in the time zone you need, generally it works off the bat with the default time zone
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("yourtz"));
cal.setTime(currentDate);
//Get the milliseconds since epoch time
long millis = cal.getTimeInMillis();
You can save this timestamp and easily sort it. It'll be more accurate and easy to use than a string and potentially gives you the ability to handle different time zones.
You can retrieve the date by setting this timestamp directly in the calendar and getting a date from it
cal.setTimeInMillis(timestamp).getTime();
Hope this helps
Hello I am developing an application using parse.When I save an object there are some fields updated autamatically like createdAt and updatedAt which saves the date and time at which the object was created or updated.When I am creating or updating a parse object I get the date and time not the date of my computer and my computer is having a correct date and time still the createdAt and updatedAt fields are not containing the accurate values.
Parse use GMT+00 timezone for Date fields
All dates stored on Parse are stored in UTC. so make sure you convert them to correct time zone
Date p =pObject.getCreatedAt();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
p = sdf.parse(sdf.format(p));
sdf.setTimeZone(TimeZone.getTimeZone("IST"));
System.out.println(sdf.format(p));
I have date like this in string datatype from server 2012-09-25 12:44:50.000. How can i change into date and store in sqlite in android....
I have tried like this...
String ackwardDate="2012-09-25 12:44:50.000";
Calendar calendar = Calendar.getInstance();
String ackwardRipOff = ackwardDate.replace("/Date(", "").replace(")/", "");
Long timeInMillis = Long.valueOf(ackwardRipOff);
calendar.setTimeInMillis(timeInMillis);
values.put(DBCREATIONDATE,calendar.getTime().toGMTString());
but i am getting Invalid Long : "2012-10-30T03:12:53.827"
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = sdf.parse("2012-09-25 12:44:50.000");
long millisec = date1.getTime();
System.out.println(""+millisec);
What type of data type you define in your sqlite database for store datetime?. If it is Text type then you need to convert this data value to string.
SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:
`TEXT` as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
`REAL` as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
`INTEGER` as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.
Applications can chose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions.
I insert date in sqlite database in a supported format (yyyy-mm-dd).
Now I would like the user at the time of the query to display the date format according to country (Italy dd-mm-yyyy, America yyyy-mm-dd etc ...)
How do I? Thanks
SQLite does not have a dedicated date and time data type. If you insert something like "01-01-2013" it will be stored like that, as a string, making comparisons, sorting and queries difficult and slow because you need to run conversions on that using SQLite date functions.
You should store UNIX timestamps instead. That requires the date column to be of type INTEGER. Timestamps can be quickly handled, sorted and selected and you can represent them in any date format you wish by using Java's Calendar and DateFormat classes, for example. You can retrieve an appropriate format for the user's default locale through factory methods.
On top of that there's Android's dedicated DateUtils class that provides various functions for creating date-time and time range strings in the user's locale.
You can also use the SQLite date and time formatter, something like:
SELECT strftime( '%d-%m-%Y', birthday) as birthday FROM people
Try the following code,
String date = "2013-11-15"; // Retrived date in your specified format from sqlite
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date res = null;
try {
d = (Date)sdf.parse(date);
} catch (Exception ex) {
ex.printStackTrace();
}
Calendar c = Calendar.getInstance();
c.setTime(d);
String day = c.get(Calendar.DAY_OF_MONTH);
String month = c.get(Calendar.MONTH);
String year = c.get(Calendar.YEAR);
//You can use these day, month and year string values to view in any format
Hope this will help you. Thank you.
I am returning data from a sqlite3 database the some else wrote. I am trying to retrieve the date and time. To store it into the db I converted the date and time into one int. using this line
int currentTime=(int) ((newTime).toMillis(true) / 1000);
I am able to retrieve the data as an int, but cannot figure out how to convert the number back into a
date and time. Currently the db returns int 13333380180, I am trying to convert it to today's date and time.
long yourmilliseconds = 13333380180;
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");
Date resultdate = new Date(yourmilliseconds);
System.out.println(sdf.format(resultdate));
Use the above code to convert
The number you're receiving is the number of milliseconds that have passed since January 1st, 1970. It's a standard, consistent way of representing a date & time on a number of platforms.
Although the value is an integer in SQLite, SQLite integers can be up to 64 bits. So, the value being returned to you should be treated as a long in Java. You can convert it to a Java Date object in a straightforward fashion: new Date(currentTime).