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.
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 have date String in the following format - eg.:
Thu, 17 Mar 2016 19:30:25 +0000
Sun, 06 Mar 2016 12:43:13 +0000
I want to convert this date into a more readable format:
Thu, 17 Mar 2016
Sun, 06 Mar 2016
public static String getMoreReadableDateFormat(String dateStringToConvert) {
SimpleDateFormat dateFormat = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");
Date convertedDate;
try {
convertedDate = dateFormat.parse(dateStringToConvert);
} catch (ParseException e) {
// could not convert date, return the initial form
return dateStringToConvert;
}
String formattedDate = new SimpleDateFormat("E, dd MMM yyyy").format(convertedDate);
return formattedDate;
}
Strange is that this code works fine for me (I get the simplified date version), but for some other peoples from other countries is not working and cannot convert date string into the simplified version. I know it has to be related to Locale, but don't know how to fix this.
You can try to set your phone's date format to french, or something else to reproduce the behavior you mentioned.
Are you sure that dateStringToConvert parameter is always in a correct format ?
I would suggest you change the first line to:
SimpleDateFormat dateFormat = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
It's important to note here the difference between DateFormats like Date, Time, and Date and Time as listed in the link below. Given your block of code, it looks like you expect a Date and Time string to be passed in and you expect to return just a Date. I'm guessing that it's not following the format that you declare as
new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z")
Also, it doesn't look like that string matches the Predefined Formats. You can try reformatting it to use
DateFormat dateAndTimeFormatter = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, currentLocale);
This would accept a string like
"Tuesday, June 30, 2009 7:03:47 AM PDT"
parse the parameter using the formatter above and then convert a different dateFormatter
DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT, currentLocale);
you can get the default local from
Locale.getDefault()
So your code would look something like this
public static String convertDate(String dateStringToConvert) throws ParseException {
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.getDefault());
Date convertedDate = dateFormat.parse(dateStringToConvert);
return DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.getDefault()).format(convertedDate);
}
Check here for more information related to using predefined formats.
https://docs.oracle.com/javase/tutorial/i18n/format/dateFormat.html
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);
I am obtaining the Date and converting into the GMT format. I am obtaining it in the form of Thu Jul 24 06:55:56 GMT+05:30 2014. I want the date to be displayed in the following form 6/19/2014 12:28:44 PM. Can anyone tell me step by step how to do it. I read the following document http://developer.android.com/reference/java/text/SimpleDateFormat.html but the format tends to remain the same even if I use a and L. I am posting the code below, please guide me.
SimpleDateFormat dateFormatGmt = new SimpleDateFormat("LL/dd/yyyy HH:mm:ss a");
dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
SimpleDateFormat dateFormatLocal = new SimpleDateFormat("LL/dd/yyyy HH:mm:ss a");
//Time in GMT
try {
dateFormatLocal.parse( dateFormatGmt.format(new Date()) );
Log.i("gmt time",""+dateFormatLocal.parse( dateFormatGmt.format(new Date()) ));
date_edittext.setText(""+dateFormatLocal.parse( dateFormatGmt.format(new Date()) ));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
use this :
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", Locale.getDefault());
say if it works :)
SimpleDateFormat format = new SimpleDateFormat("MMM dd,yyyy hh:mm a");
String date = format.format(Date.parse("Your date string"));
String strCurrentDate = "Wed, 18 Apr 2012 07:55:29 +0000";
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss Z");
String currentDate = format.format(strCurrentDate);
format = new SimpleDateFormat("MMM dd,yyyy hh:mm a");
String date = format.format(Date.parse(currentDate));
I don't quite get your problem but look :
Date test = new Date();
test.setHours(10);
SimpleDateFormat sdf = new SimpleDateFormat("M/dd/yyyy HH:mm:ss a");
System.out.println(sdf.format(test));
This displays 7/24/2014 10:13:01 AM
If I change setHours to test.setHours(22) this displays 7/24/2014 10:13:01 PM
Hope this helps.
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