how to convert this datetime format into unix timestamp - android

I tried to parse this date 2017-03-04**T**17:20:55Z to Unix timestamp and I received a parse error for the T char (bolded it).
I tried to use new SimpleDateFormat and DateTimeFormatter. How can I get a Unix timestamp from this DateTime format?

You can use single quotes to escape characters in the format string of SimpleDateFormat:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Date date = dateFormat.parse("2017-03-04T17:20:55Z");

Related

Joda time convert ISO8601 GMT to local

I have this from my server:
2017-04-07T11:00:00.000Z
How can I convert it to local time? Here is my code, but all I got is an exception:)
java.lang.IllegalArgumentException: Invalid format: "2017-04-07T11:00:00.000Z" is malformed at "Z"
DateTimeFormatter inputFormatter = DateTimeFormat
.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ") //yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ
.withLocale(Locale.US);
DateTime parsed = inputFormatter.parseDateTime(gmtDate);
DateTimeFormatter outputFormatter = DateTimeFormat
.forPattern("HH:mm")
.withLocale(Locale.US)
.withZone(DateTimeZone.getDefault());
return outputFormatter.print(parsed);
Just change your pattern to yyyy-MM-dd'T'HH:mm:ss.SSSZ, like this:
DateTimeFormatter inputFormatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
.withLocale(Locale.US);
In my case, the result of outputFormatter.print(parsed) is 08:00 (my local time, as my default timezone is America/Sao_Paulo)

java.lang.NumberFormatException: Invalid date/time format: 2016-01-21T00:57:03.126+0500

I am setting date and time using this method...
calendar.set(year,monthOfYear,dayOfMonth);
calendar.set(Calendar.HOUR_OF_DAY,hourOfDay);
calendar.set(Calendar.MINUTE,minute);
When fetching DateTimeStamp, and setting in Event object, I am facing an error
NumberFormatException: Invalid date/time format:
String format = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
DateTime startDateTime = new DateTime(sdf.format(calendar.getTime()));//This Line causing it
EventDateTime start = new EventDateTime().setDateTime(startDateTime).setTimeZone(calendar.getTimeZone().toString());
event.setStart(start);
Problem was solved by using "yyyy-MM-dd'T'HH:mm:ss'Z'" this format.
As DateTime accepts RFC3339 where zone ('z') is written in quotes

How do I create a date field in sqlite and define the date format for it?

I want to store a date field into my database. In order to do that, I think I might need a format to store inside the database.
How can I set the format for that date field? And in the java code part, the variable I should used, is it a Integer? Or just a String?
you can use DATE field as varChar i.e in String format.
ex : add this to your table for date COLUMN_varDisplayDate + " Text ,"
use this method to format date as per your requirements,
public static String getDateFormat(String date) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
String convertedDate = "";
try {
Date parseDate = dateFormat.parse(date);
SimpleDateFormat fmtOut = new SimpleDateFormat("yyyy-MM-dd");
convertedDate = fmtOut.format(parseDate);
} catch (ParseException e) {
e.printStackTrace();
}
return convertedDate;
}
Allowed Date-Time format in SQLite:
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
DDDDDDDDDD
How can I set the format for that date field?
Use SimpleDateFormat to format your exiting Date Object or Date String object as above specified format.
variable I should used, is it a Integer? Or just a String?
String
You can store date in form of number of seconds(since Jan 1 , 1970).
It can be stored in Integer format. for getting date in form of seconds you can use getTimeInMillis() function of Calendar class which gives you time in milliseconds and divide it by 1000.
For displaying it in user readable format you can just format that date depending upon which database you are using.
for Sqlite you can use strftime function to retrieve date in desired format.
http://www.w3resource.com/sqlite/sqlite-strftime.php

Convert a string date to another date format

I have a date from a server in format 01/01/2012 12:00:00PM and I want to convert that date to ISO8601 format but I keep getting a Parse Exception when I try to parse the date
my code
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmZ");
Date timestamp = null;
try{
timestamp = format.parse(startDate);
startDate = format.format(timestamp);
}catch(ParseException e){
}
First parse your String and create Date object with incoming format.
DateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a");
Then create another date format with the format you are looking for and format the date object.
DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmZ");
startDate = format2.format(timestamp);

Convert JSON date in the "mm dd yyyy" format

In my application the Date Fiend JSON response comes as this type:
"CreatedOn": "\/Date(1313572467987+0000)\/"
I want to convert this date in the "MM DD YYYY" format. How can I convert this?
That date in your JSON response looks like a standard timestamp (e.g. number of milliseconds since January 1, 1970). If you parse out the timestamp something like:
String timestamp = jsonValue.split("\\(")[1].split("\\+")[0];
Date createdOn = new Date(Long.parseLong(timestamp));
Now you can use SimpleDateFormat to format a date string:
SimpleDateFormat sdf = new SimpleDateFormat("MM dd yyyy");
String formattedDate = sdf.format(createdOn);
This is ignoring the timezone adjustment in that response (the '+0000') you might also want to parse out this value and add/remove the hours from your timestamp value before formatting..
you can use SimpleDateFormat, so just try to search on the same.
Detailed example is here: http://www.helloandroid.com/tutorials/date-handling-android-development

Categories

Resources