convert a date string into long in Android - android

I have a date string: 02/28/2013 06:20:00 PM
I have a date formater: SimpleDateFormat dateFmt = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a");
java.util.Date tempDate;
when I parse the string with the formater and extract the date
tempDate = dateFmt.parse(xpp.getText());
I get a date that is off by 12 hours: Thu, Feb 28, 2013 6:20 AM
What am i overlooking?

The javadocs for SimpleDateFormat say that HH is for the hour in 24-hour format (0-23). For AM/PM format (1-12) you should use hh.

Related

Convert GMT+ date to local on android

I need to convert date like Mon Nov 14 13:36:01 GMT+03:00 2016 to local. So it must be 16:36:01
Date serverDate; // Mon Nov 14 13:36:01 GMT+03:00 2016
SimpleDateFormat dateFormat = new SimpleDateFOrmat("hh:mm:ss");
String result = dateFormat.formatDate(serverDate);
Result doesn't see GMT +03:00 and gives me wrong time (13:36 instead of 16:36).
So I just needed to create formatter without ZZZZZ and set time zone to GMT.
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault());
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
final Date localDate = simpleDateFormat.parse("2016-11-11T09:48:24-05:00");
SimpleDateFormat dateFormat = new SimpleDateFOrmat("hh:mm:ss");
String result = dateFormat.formatDate(localDate); //12:48:24
After this code time of localDate variable will be 12:48:24 (in case of my GMT +03:00).

Cannot parse date string containing three letter month

I spent about hour to solve problem, but I couldn't...
My date string is "06 Jan 2016", and I want to parse it to object Date.
I tried next method
SimpleDateFormat frmt2 = new SimpleDateFormat("dd MMM yyyy");
Date date = frmt2.parse("06 Jan 2016");
And I got:
java.text.ParseException: Unparseable date: "06 Jan 2016" (at offset 3)
I tried Joda lib
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd MMM yyyy");
DateTime dt = formatter.parseDateTime("06 Jan 2016");
But I got same error:
Invalid format: "06 Jan 2016" is malformed at "Jan 2016"
Can you help me please to obtain the success in this simple problem.
Thank you very much.
SimpleDateFormat format = new SimpleDateFormat("MMM dd,yyyy hh:mm", Locale.ENGLISH);
Date theDate = format.parse("JAN 13,2014 09:15");
Calendar myCal = new GregorianCalendar();
myCal.setTime(theDate);
System.out.println("Day: " + myCal.get(Calendar.DAY_OF_MONTH));
System.out.println("Month: " + myCal.get(Calendar.MONTH) + 1);
System.out.println("Year: " + myCal.get(Calendar.YEAR));

Simple date formatting giving wrong time

I am trying to parse a String to a Date and it giving me right date where as time is wrong.
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm aaa");
try {
Date date = formatter.parse("2015-08-20 05:00 AM");
Log.e("date", date+""); // Logcat printing Thu Aug 20 00:00:00 GMT+05:30 2015
} catch (ParseException e) {
Log.e("Error",e.toString());
}
As you can see irrespective of the time every time parsed date time showing 00:00:00
What I want is Thu Aug 20 05:00:00 GMT+05:30 2015
It seems the problem was that your pattern String specified am/pm, but was using uppercase H's for the hour characters. These indicate a 24-hour clock, which obviously doesn't use am/pm. Change the hour characters to lowercase h's, which indicate the hour in am/pm (0-11).
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm aaa");
The docs for SimpleDateFormat explain the various acceptable pattern characters.
Try below code
SimpleDateFormat format = new SimpleDateFormat("E, dd MMM yyyy hh:mm:ss z");
String dateToStr = format.format(new Date());
System.out.println("dateToStr=>" + dateToStr);
try {
Date strToDate = format.parse(dateToStr);
System.out.println("strToDate=>" + strToDate);
} catch (ParseException e) {
e.printStackTrace();
}
Maybe its related to time zone issues, at what time zone is your input?, i'd suggest to make sure your paramater is on UTC timezone and then using formatter like this :
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm aaa");
formatter .setTimeZone(TimeZone.getTimeZone("UTC"));
Try this code you missed some lines of code
SimpleDateFormat mFormatter = new SimpleDateFormat("yyyy-MM-dd hh:mm aa", Locale.getDefault());
mFormatter.setTimeZone(TimeZone.getTimeZone("India"));
Date startDate = mFormatter.parse("2015-08-20 05:00 AM");
This code working fine for me.

How to convert time in 12 hr format?

I'm getting call time through a cursor and want to display it in 12 hr format instead of 24 hr format.
Here is my code to get time from a cursor
String callDate = managedCursor.getString(dateIndex);
Date callDayTime = new Date(Long.valueOf(callDate));
and set this call day time to text view by
lastintreactionvalueTV.setText(callDayTime+"");
it's showing it like
Thu Jun 26 14:36:24 EDT 2014
What should I do to convert it into 12 hr format?
Use SimpleDateFormat to set the format that you need, for example:
Date callDayTime = new Date();
DateFormat sdf= new SimpleDateFormat("EEE, MMM dd KK:mm:ss a z yyyy",new Locale("en"));
System.out.println(sdf.format(callDayTime) );
This will output:
Thu, Jun 26 08:54:51 AM CEST 2014
Use this,
SimpleDateFormat read = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat write = new SimpleDateFormat("MMMM-dd-yy");
String op = write.format(read.parse("2014-05-17 15:45:56"));
//
SimpleDateFormat read = new SimpleDateFormat("input format");
SimpleDateFormat write = new SimpleDateFormat("output format");
String op = write.format(read.parse("your date as in input format"));

Date Parse converting PM to AM

In the following code myLeg.TimeStamp is a String that has "Feb 26 2014 12:31:23 PM"
myRTleg.Tstamp is a Date.
SimpleDateFormat formatter = new SimpleDateFormat("MMM d, yyyy HH:mm:ss a");
myRTleg.TStamp = formatter.parse(myLeg.TimeStamp);
String debugStr = myRTleg.TStamp.toString();
DebugStr has the same exact date in it but it has AM instead of PM !!
Why is it doing this?
Thanks,
Dean
From
http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
H Hour in day (0-23)
...
h Hour in am/pm (1-12)
so try
SimpleDateFormat formatter = new SimpleDateFormat("MMM d, yyyy hh:mm:ss a");
The AM/PM is probably defaulting to AM, and being ignored because the hours are HH (military) instead of hh
You didn't provide the full input and output dates. Check the timezone of your input date and output date to see if they are different. It could just be the same date in time but formatted in using different time zones.
You need to use hh for hours instead of HH. HH is used for Military time.

Categories

Resources