Date Parse converting PM to AM - android

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.

Related

SimpleDateFormat with Timezone names in different languages

I have encountered this strange problem when trying to parse date strings, which I have nailed down to this:
Device with some English locale (the emulator):
Date.toString() gives "Thu Feb 18 13:25:22 GMT 2016"
DateFormat newDateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
d1 = newDateFormat.parse("Thu Feb 18 13:25:22 GMT 2016"); //works fine
Device with German locale:
Date.toString() gives "Thu Feb 18 13:25:22 MEZ 2016" (note the timezone in German, while Thursday is still Thursday)
DateFormat newDateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
d1 = newDateFormat.parse("Thu Feb 18 13:25:22 MEZ 2016"); //does not parse
DateFormat newDateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.GERMAN);
d1 = newDateFormat.parse("Thu Feb 18 13:25:22 MEZ 2016"); //does not parse
DateFormat newDateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
String s = "Thu Feb 18 13:25:22 MEZ 2016";
s.replace("MEZ", "CET");
d1 = newDateFormat.parse(s); //works fine, but doesn't seem very portable
So I am looking for a way to either get SimpleDateFormat to ignore the time zone completely (that would be fine for my app, the only thing I am trying to do here is give a time difference to "last time", which will be given in hours if it's less than a day, but in days or weeks if it's more, so the time zone won't matter that much for this use case),
or for a way to get SimpleDateFormat to understand timezones in the device language.
Bonus points for a good way to save the date in a language-independent way, while still retaining information such as timezone and time of day in that time zone. (for when I upgrade my database next time)
I did it in this way to get the current date in YYYY-MM-DD format.
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
String CurrentDate = formatter.format(new Date());
Date current_date = formatter.parse(CurrentDate); //In case need the entire Date value
Hope this helps!
To get your desired result (which i believe looks something like: Mi. März 15:15:58 02-03-16 MEZ)
I used to the following code:
public static final String DATE_FORMAT = "EEE MMM HH:mm:ss dd-MM-yy z";
private DateTimeFormatter mDateFormatter = DateTimeFormat.forPattern(DATE_FORMAT).withLocale(Locale.GERMAN);
/**
* Get formatted Date as Human Readable String
* #param dateTime the time to print
* #return Date as String
*/
public String getFormattedDate(#NonNull DateTime dateTime) {
return mDateFormatter.print(dateTime);
}
If you want a Time difference, you might wanna take look at JodaTime (http://www.joda.org/joda-time/). I've used it myself, it provides simple interfaces for time differences (Days, Hours, Minutes, Months, Seconds, Weeks, Years).
Save your Dates in Unix Time (ISO 8601), its all the information you need.
When you are instantiating SimpleDateFormat,
DateFormat newDateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
SimpleDateFormat has a constructor that takes in a locale, SimpleDateFormat(String) constructor has been deprecated now, you should use the constructor that takes in the locale too like so,
DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.getDefault());
Now if you use this df.parse(myDateString) you don't need to do string replace from MEZ to CET because the SimpleDateFormat knows your locale will parse the date properly

android convert utc time in 24 hours format

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"

What is the date format String for 29 Sep, 30 Sep, 02 Oct etc

I tried the following code for this.
private SimpleDateFormat dateFormatter = new SimpleDateFormat("d MMM");
dateFormatter.parse("13 Jan");
but it raising parse exception invalid date.
This is the dd MMM format. So you need to use it as
new SimpleDateFormat("dd MMM");
Read Date and Time Patterns documentation for more details.
There might another issue. For your current input, format "d MMM" will also work.
Try it as:
final Calendar calendar = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM"); // dd for date, MMM for Month
Toast.makeText(getApplicationContext(), formatter.format(calendar.getTime()), Toast.LENGTH_SHORT).show();
Hope it helps.

formatting date in android

I'm trying to display date which I already have to a new format, i'm using SimpleDateFormat for this.
Android Code
String date = "2013-08-11 20:38 EDT";
SimpleDateFormat sf = new SimpleDateFormat("dd MMMM yyyy hh:mm a");
try {
newDate = sf.format(new SimpleDateFormat("yyyy-MM-dd kk:mm z").parse(date));
} catch (ParseException e) {
e.printStackTrace();
}
It displays :
11 August 2013 08:38 PM
However if I run the same code in JAVA (as a normal JAVA console application)
JAVA
String date = "2013-08-11 20:38 EDT";
SimpleDateFormat sf = new SimpleDateFormat("dd MMMM yyyy hh:mm a");
String lDate = sf.format(new SimpleDateFormat("yyyy-MM-dd kk:mm z").parse(date));
System.out.println(lDate);
It displays :
12 August 2013 06:08 AM
This is the format which I need to display.
P.S. : I got a warning in android saying
To get local formatting use getDateInstance(), getDateTimeInstance(), or getTimeInstance(), or use new SimpleDateFormat(String template, Locale locale) with for example Locale.US for ASCII dates.
so I tried adding Locale.US to SimpleDateFormat it again show the 11 August 2013 08:38 PM and not 12 August 2013 06:08 AM
My Question is :
how to display date as 12 August 2013 06:08 AM in android.
Your local time IST is UTC+05:30, your Android time EDT is UTC-04:00. Together they add to 9h30min of difference explaining the difference in output.
Set your Android device to IST timezone to get the same output.
Alternatively, you can call setTimeZone() on the DateFormat to explicitly set a timezone to use.
It is also helpful to explicitly print timezone information to make datetime stamps less ambiguous.
new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.S", Locale.US)
You should specify the time zone
try this code
SimpleDateFormat simple = new SimpleDateFormat("dd MMMM yyyy hh:mm a" , java.util.Locale.getDefault());

convert a date string into long in 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.

Categories

Resources