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
Related
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");
One of the attributes in JSON have date value ("/Date( -62135575200000-0600)/"), I'm not familiar with the type. How to convert this string to SimpleDateFormat
JSON attribute:
"toDate": "/Date( -62135575200000-0600)/"
This should work:
String date = "-62135575200000-0600"; // strip down the numerical value
Date date1 = new Date(date);
DateFormat fmt = new SimpleDateFormat("yyyy mm dd");
System.out.println(fmt.format(date1));
Negative date is taken as milliseconds before 1970 jan 1.
Source
I am receiving date and time format as mentioned below
2016-04-13T00:12:33+05:30
i need to convert above format to HH:MM:SS AM/PM
can any one guide me,your response will be appreciated......
Android has the DateFormat class for parsing and reformatting dates as Strings, but JodaTime is much better. JodaTime is small and can handle what you're looking for in just a couple lines of code.
You need first String to Date using Simple forma tor
Then date need to change to formatResult fro expected result
SimpleDateFormat format = new SimpleDateFormat("dd-M-yyyy hh:mm:ss", Locale.ENGLISH);
SimpleDateFormat formatResult = new SimpleDateFormat("hh:mm aa");
SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss a");
Log.i("date ====== " + formatter.format("2016-04-13T00:12:33+05:30"));
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
In my app i need to convert my current date into UTC format, i can successfully converted, now problem is i need 24 hours format check it out my below code
public static String CurrentDate() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
LogUtil.d("new Date()" + new Date());
return df.format(new Date());
}
now my CurrentDate returns 1.45 but i need 13.45 how can i convert utc in 24 hours format?
i have search through google but dint get proper answer, all suggestions are most welcome
Thanks in advance
Changing the hour part at your SimpleDateFormat constructor call from hh to HH does the job:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Output:
2015-07-13 13:53:02
See also Table of Date and Time Patterns
As Jon Skeet said, check the String you pass on SimpleDateFormat() : http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
You need to replace "hh" with "HH" so it will become "yyyy-MM-dd HH:mm:ss"
Of course if you only need the hour and minute that should be "HH:mm"