I'm having a time zone issue with my SimpleDateFormat. Here is my code:
TextView date_time = (TextView) view.findViewById(R.id.date_time);
SimpleDateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.US);
Date date = null;
try {
date = df.parse(workout.getDate().toString());
System.err.println("position: " + position + " workout: " + workout.getDate().toString());
System.err.println("position: " + position + " date: " + date);
} catch (ParseException e) {
e.printStackTrace();
}
df = new SimpleDateFormat("EEEE, MMMMM dd", Locale.US);
date_time.setText(df.format(date));
Note this output:
position: 0 workout: Mon Aug 19 00:00:00 MDT 2013
position: 0 date: Sun Aug 18 23:00:00 MDT 2013
Do you see how the string starts (correctly) with the date being Aug 19 (at midnight). But then after the date formatter does its work, I come out with the time being 1 hour earlier. I'm assuming that this is some time zone manipulation, but I don't know how to correct for it. I tried some different values for 'Z' (including 'Z', 'ZZZ', 'ZZZZ', and 'ZZZZZ'), but all give the same result. I assume that it's a time zone problem, but in both cases is shows 'MDT', so maybe not.
How do I stop this one hour shift from happening? Thanks!
EDIT: VERY hacky solution but it works:
try {
date = df.parse(workout.getDate().toString());
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.HOUR_OF_DAY, 1);
date = cal.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
Related
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));
I have a problem. I am using this code to "increase" or "decrease" one day. "value" is 1 or -1. I have tried c.add(Calendar.DAY_OF_MONTH, value); and c.add(Calendar.DATE, value); The problem is:
if value = 1, it increases day by 1 and month and year don't budge at all and remain same.
if value = -1, it decreases day by 1 and after 30 days it decrease the year.
I have tried everything but could not understand the behavior. I need to increase or decrease the whole date like it happens in MYSQL, not just date or month or year.
SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
Calendar c = Calendar.getInstance();
try {
c.setTime(sdf.parse(mDate));
} catch (ParseException e) {
e.printStackTrace();
}
c.add(Calendar.DAY_OF_MONTH, value);
sdf = new SimpleDateFormat("dd/mm/yyyy");
Date resultdate = new Date(c.getTimeInMillis());
String newDtString = sdf.format(resultdate);
You can try
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + value);
You'd also have to handle end cases when it's first or last day of year
Java handles this for you. Example:
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy");
calendar.setTimeZone(TimeZone.getDefault());
try {
calendar.setTime(sdf.parse("31 December 2015"));
} catch (ParseException e̋̋) { }
System.out.println(sdf.format(calendar.getTime()));
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + 1);
System.out.println(sdf.format(calendar.getTime()));
Prints:
31 December 2015
01 January 2016
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.
I was trying to converted user inputed date in GMT time, user inputed date in 24hour format, when i convert in GMT time it show 12 hour problem, How can i get 24 hour formated GMT time
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String s = starttDatePicker.getYear() + "-"
+ (starttDatePicker.getMonth() + 1) + "-"
+ starttDatePicker.getDayOfMonth() + " "
+ starttimepicer.getCurrentHour() + ":"
+ starttimepicer.getCurrentMinute() + ":" + "00";
Date parsed = null;
try {
parsed = format1.parse(s);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
TimeZone tz = TimeZone.getTimeZone("GMT");
format1.setTimeZone(tz);
posttime = format1.format(parsed);
Change "yyyy-MM-dd hh:mm:ss" to "yyyy-MM-dd HH:mm:ss"
Just to clarify the documentation for formatting date/times mentions that hh is 12 hour time while HH is 24 hour time.
hh is for 12 hrs format and
HH is for 24 hrs format
For some reason this has me tearing my hair out.
I have a UNIX timestamp as a string in Android. All I want to do is format this so that it returns the date/time in the user's droid time zone.
I can convert it to a timestamp just fine, but it uses GMT rather than their localised zone.
Thanks
Use the SimpleDateFormat constructor with the Locale you need:
http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html#SimpleDateFormat%28java.lang.String,%20java.util.Locale%29
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
Calendar c = Calendar.getInstance();
try {
Date dt = sdf.parse("2011-03-01 17:55:15");
c.setTime(dt);
System.out.println( c.getTimeInMillis());
System.out.println(dt.toString());
} catch (ParseException e) {
System.err.println("There's an error in the Date!");
}
outputs:
1299002115000
Tue Mar 01 12:55:15 EST 2011