I'm tryint to transform "Wed, 22 Apr 2015 05:45:42 GMT" to "yyyy MM dd - HH:mm"
But that string does not fit in all these simpledateformats, it gives exception in all three
new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss");
new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss 'Z'");
I can reproduce your problem since I am sitting in a non-english-speaking country, too. The solution is to specify English as language because your input contains an English word/abbreviation (look at the day of week "Wed"!). Following solution works:
SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
Date d = sdf.parse("Wed, 22 Apr 2015 05:45:42 GMT");
SimpleDateFormat out = new SimpleDateFormat("yyyy MM dd - HH:mm", Locale.ROOT);
System.out.println(out.format(d)); // output in the system timezone
It does not matter what your app is for (worldwide or not). The only fact which matters is the language of your text input. If this is varying in language then and only then you have first to determine the language of the device in question (probably Locale.getDefault()) and set it on your SimpleDateFormat-object.
Use small z in date format
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'z'");
I have checked it with below code, it is working.
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
try {
Date date =simpleDateFormat.parse("Wed, 22 Apr 2015 05:45:42 GMT");
Log.e(MainActivity.class.getSimpleName(),"date : "+date.toString());
} catch (ParseException e) {
e.printStackTrace();
}
Related
I am trying to convert the following data time to EEE, d MMM yyyy format but kept getting Unparsable date format. Can some one please help. I couldn't find anything on the web that helped.
Here is the code
String datestr = "2017-01-12T00:00:00Z";
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US);
Date convertedDateStart = new Date();
try {
convertedDateStart = dateFormat.parse(datestr);
camp_new.startdate = convertedDateStart;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Any help wound be greatly appreciated. Thanks in advance
The date string "2017-01-12T00:00:00Z" cannot be parsed with your format "EEE, d MMM yyyy HH:mm:ss Z"; the fields are the wrong type or in the wrong place.
The format for your datestr "2017-01-12T00:00:00Z" should be:
yyyy-MM-dd'T'HH:mm:ssZ
You need to initialize the SimpleDateFormat with this format to parse datestr.
The format "EEE, d MMM yyyy HH:mm:ss Z" would parse a date that looks like
Thu, 1 Jan 2017 00:00:00 Z
Read the docs to understand the difference between parse() and format().
You have to parse it first, store it as a Date object and then use another SimpleDateFormat to format in the desired way.
String datestr = "2017-01-12T00:00:00Z";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US);
SimpleDateFormat dateFormat2 = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US);
Date date = dateFormat.parse(datestr);
String convertedDateStart = dateFormat2.format(date);
Well This will Work For you if you want to FORMAT and get a Date String with that format..
String datestr = "2017-01-12T00:00:00Z";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
try {
date = dateFormat.parse(datestr);
} catch (ParseException e) {
e.printStackTrace();
}
dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US);
datestr = dateFormat.format(date);
System.out.println(datestr);
This will give you Thu, 12 Jan 2017 00:00:00 +0430 Date String.
I am trying to parse a date in android using the following code
String dateString = "Sun Apr 15 13:37:33 CEST 2012";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
System.out.println(sdf.parse(dateString));
The code above works fine with java, but in Android it gives ParseException.
java.text.ParseException: Unparseable date: "Sun Apr 15 13:37:33 CEST 2012" (at offset 20)
What could be the possible reason?
I am running it on Samsung S4, lollipop, system language is French, and system timezone is GMT+2:00.
try this code:
String mytime="Jan 17, 2012";
SimpleDateFormat dateFormat = new SimpleDateFormat(
"MMM dd, yyyy");
Date myDate = null;
try {
myDate = dateFormat.parse(mytime);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat timeFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss
zzz yyyy");
String finalDate = timeFormat.format(myDate);
TextView t = (TextView) findViewById(R.id.txt);
t.setText(finalDate);
I try to parse date like: Tue, 08 Sep 2015 12:32:55 +0300 and appy next pattern:SimpleDateFormat inputFormat = new SimpleDateFormat("EEE, d MMM yyyy hh:mm:ss zzzz");
But I catch exception java.text.ParseException: Unparseable date: "Sun, 13 Sep 2015 17:25:26 +0300" (at offset 0)
Try changing the code to
SimpleDateFormat inputFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss zzzz", Locale.US);
Edit:
Additional amendment I have changed hh in your code to HH for 24 hours format
I have a string variable which contains date and time in the following format
EDIT:
String newdate="Tue Mar 04 16:58:00 GMT+05:30 2014";
I need to covert and parse it into this date format using simpledateformat as below:
Date nd=04-03-2014 16:58:00
But I don't know the string pattern for converting using simpledateformat.
I have given as
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-dd-MM hh:mm:ss");
But it gave me wrong output.
Is the string pattern for simpledateformat sdf correct? If I'm wrong can someone please correct me in converting newdate to date nd format.
Try to use
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
But before conversion replace "." on ":" in "GMT+05.30"
Example:
String newdate="Tue Mar 04 16:58:00 GMT+05:30 2014";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
try {
Date d = sdf.parse(newdate);
} catch (ParseException e) {
e.printStackTrace();
}
Try
SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-YYYY hh:mm:ss");
Your date format is not correct.
Try:
"Tue Mar 04 16:58:00 GMT+05:30 2014";
instead of:
"Tue Mar 04 16:58:00 GMT+05.30 2014";
It seems that SimplaDateFormat this GMT+05:30 part works only with ':'.
Than you can parse it like that:
String newdate="Tue Mar 04 16:58:00 GMT+05:30 2014";
SimpleDateFormat sdf=new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy" );
Date nd = sdf.parse(newdate);
First of all, your date string is not correct, which should be
String newdate="Tue Mar 04 16:58:00 GMT+05:30 2014";
not this...
String newdate="Tue Mar 04 16:58:00 GMT+05.30 2014";
you placed Dot (.) in GMT+05.30 instead of Colon (:) as GMT+05:30.
Now, Use this format...
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
For Exmaple:
Date date;
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
try {
date = formatter.parse(newdate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String convertedDate = new SimpleDateFormat("dd-MM-yyyy HH:mm").format(date);
Log.i("ConvertedDate", convertedDate);
OutPut:
03-04 19:39:49.959: I/ConvertedDate(310): 04-03-2014 17:28
I have a sample date format Tue, 23 Aug 2011 18:33:43 +0000
In Android, I am using this statement String now = new Date().toGMTString();
And I am receiving this 23 Aug 2011 18:03:25 GMT
What I need to do to get this format Tue, 23 Aug 2011 18:33:43 +0000?
You can use SimpleDateFormat to parse and format dates.
Use SimpleDateFormat.
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
String formattedDate = dateFormat.format(yourDateInstance);
Follow #MByD, except try this:
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT");
String formattedDate = dateFormat.format(yourDateInstance);