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
Related
From API i get time as string "13:00:00Z" in this format and it is GMT 0 time zone.
I want to adjust it to my time zone which is GMT +4.
First you convert time into utc then after convert utc time to your local time zone. like example:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault());
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
try {
Date utcDate = simpleDateFormat.parse("Your date");
SimpleDateFormat yourDateFormat = new SimpleDateFormat("dd MMM yyyy ยท HH'h'mm", Locale.getDefault());
String dateString = simpleDateFormat.format(utcDate);
binding.orderdate.setText(dateString);
} catch (Exception e) {
e.printStackTrace();
}
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 have a JSON that get a Date as dd-mm-yyyy, but I need convert that String to Date, but when I do the SimpleDateFormat always returns Sat May 02 00:00:00 EDT 2015 (for example) and I want 02-05-2015
this is my code:
String str = city.getString("bd_fecha");
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
date = dateFormat.parse(str); //// DATE ALWAYS RETURNS Sat May 02 00:00:00 EDT 2015(for example)
Use this, after parse you have to format also
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String dateInString = "07-06-2013";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
I have a string that contains a date like so:
String startTime = "2014-10-11T17:00:41+0000"
I am trying to reformat that string so that it reads like so instead:
Oct 11, 2014 5:00 PM
Since Date objects do not keep time zone information, you need to specifically set the time zone offset of original date to the target formatter. Here is the complete code for transforming from one format to another while maintaining the time zone offset (+0000 in your case). More information on TimeZone class here and on how to build a proper date and time pattern string for your requirement here.
try {
DateFormat originalFormat = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ssZ", Locale.ENGLISH);
DateFormat targetFormat = new SimpleDateFormat(
"MMM dd, yyyy K:mm a", Locale.ENGLISH);
targetFormat.setTimeZone(TimeZone.getTimeZone("GMT+0000"));
Date date = originalFormat.parse("2014-10-11T17:00:41+0000");
String formattedDate = targetFormat.format(date);
System.out.println(formattedDate);
} catch (ParseException e) {
e.printStackTrace();
}
Output: Oct 11, 2014 5:00 PM
Use SimpleDateFormat for parse input string and represent in new format:
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Ex.:
SimpleDateFormat sdfmtIn = new SimpleDateFormat("dd/MM/yy");
SimpleDateFormat sdfmtOut= new SimpleDateFormat("dd-MMM-yyyy");
java.util.Date date = sdfmtIn.parse(strInput);
String strOutput = sdfmtOut.format(date);
Is it possible to store the current date in a file but no as a string?
What I mean:
If I use :
SimpleDateFormat thedate = new SimpleDateFormat("dd/MM/yyyy");
Date d=new Date();
String formattedDate=thedate.format(d);
date.add(formattedDate);
It stores (with the format I want) in date list (which is a String List).
I want 'date' to be a Date List and store it to a file.
Is it possible to store it as dd/MM/yyyy ?Because like this:
SimpleDateFormat thedate = new SimpleDateFormat("dd/MM/yyyy");
Date d=new Date();
date.add(d);
it stores as
Thu Apr 18 17:06:14 GMT+03:00 2013
Try the above code:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
dateFormat.format(date);
This will not save the date as string(as you desire) and will format the current date according to your need. I haven't tested it right now, but as I have used this before so I am pretty sure this works. Hope this helps. If it does not then please comment.
Update
I tried following:
List<Date> d = new ArrayList<Date>();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date date = new Date();
String s = dateFormat.format(date);
try {
d.add(dateFormat.parse(s));
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Log.d("try","NewDateString: "+ d.get(0));
Log.d("try","NewDate: "+ d.get(0).getDate()+" "+d.get(0).getMonth()+" "+(d.get(0).getYear()+1900));
It works and following was the result I got:
NewDateString: Thu Apr 18 00:00:00 EDT 2013
New Date: 18 3 2013
So what you can do is, save the date as above and when you are retrieving the date from the file you may choose to get the Day/Month/Year from the date object that you saved.