Date is not accurate while saving parseObject - android

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));

Related

Get Firebase Timestamp

I want to know how can I get the value of the timeStamp from the ServerValue.TIMESTAMP, directly from the HashMap? I know that when that value is sent to Firebase I get the long value, but I want to get that long before send it to Firebase.
Okie so after some research I found that we cannot use ServerValue.TIMESTAMP before sending it to database because
ServerValue.TIMESTAMP is set as a Map (containing {.sv: "timestamp"}) which tells Firebase to populate that field with the server's time. When that data is read back, it is the actual unix time stamp which is a Long.
Link
By the way you can use android own timestamp and store it in Firebase.
Calendar cc = Calendar.getInstance();
Date date = cc.getTime();
// SimpleDateFormat format1 = new SimpleDateFormat("dd MMM");
SimpleDateFormat format2 = new SimpleDateFormat("yyyyMMddHHmmssSSS");
timestamp = format2.format(date);

Time incorrect after add format date in gson builder

I have json string which is returned from mongodb.
[{"_id":"578bb51aa51d15940688809e","name":"aaa","date":"2016-07-20T11:47:39.302Z"}]
I used gson to convert the Java Object. I got Unparseable date: "2016-07-20T11:47:39.302Z". So I added date format to gson builder.
Gson gson= new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").create();
The date result is Wed Jul 20 11:47:39 MMT 2016. The time is incorrect.
What is the better date format pattern for date string?
How can I choose the suitable date format pattern by the given date string?
If I am not mistaken, you are having an issue with timezone information. Both the JavaScript Date object and the Java Date object represent a point in universal coordinated time (UTC). Neither the JavaScript Date object nor the Java Date object contain timezone information.
Side note: The Java Date object does actually contain timezone information, which is why you see the timezone (MMT in your case) when you call date.toString(). Though, this timezone should be ignored, as the timezone irregularities highlight some of the many issues with the Java 7- date/time classes.
The GsonBuilder().setDateFormat() method sets the string format that a Java Date object should be serialized to and deserialized from. This format does not contain any timezone information either because the Date object does not contain that information.
In order to display the UTC time your Date object represents in the correct timezone, you must provide that timezone. This is not done during deserialization with Gson, but when you actually display the date/time:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getDefault());
String zonedDateTime = simpleDateFormat.format(date);
Android also comes with a handy DateUtils class to use instead of SimpleDateFormat that will handle the timezone information for you, as well as Locale information.
DateUtils.formatDateTime(getContext(), date.getTime(),
DateUtils.FORMAT_NUMERIC_DATE | DateUtils.FORMAT_ABBREV_ALL);

Date format issue with 'HH' when storing to SQLite Database

I'm having an issue when storing a date to my local SQLite database in my Android App.
I am using a global date format:
public static final String DATEFORMAT = "yyyy-MM-dd HH:mm:ssz";
Once I have retrieved a date from my Azure database and I then format it to a string so it can be stored in a SQLite database.
row.put(Constants.COL_DATE_START, (String) DateFormat.format(DATEFORMAT, DateStart));
It all seems to work fine except for instead of 2013-09-18 13:00:00+0000 I am getting:
2013-09-18HH:00:00+0000
For some reason it won't pick up on the 'HH' being an hour number. If I change it to 'hh'
it gives me 2013-09-18 01:00:00+0000 - leaving me 12 hours off.
Any ideas? Any help would be greatly appreciated!!
Try this:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateFormat = sdf.format(yourDate); //java.util.Date
row.put(Constants.COL_DATE_START, dateFormat);
I usually store a timestamp and format the date when I'm showing it. I know it doesn't answer your question directly but AFAIK it is considered best practice.

Android Format Date from SQLite

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.

Date and time in android

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

Categories

Resources