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);
Related
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'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();
}
"Fri, 12 Sep 2014 05:00:23 GMT",what's wrong with SimpleDateFormat?
SimpleDateFormat sdf = new SimpleDateFormat(format);
sdf.parse(dateString);
format: "EEE, dd MMM yyyy HH:mm:ss 'GMT'"
dateString:"Fri, 12 Sep 2014 05:00:23 GMT"
what's wrong ?
Assuming your problem is that the date is coming out wrong, the SimpleDateFormat conversion specifier for timezone is z rather than 'GMT' (although you can use Z and X for the other two variants, RFC822 and ISO8601 respectively).
With your specifier, I get a local time of 5am. If I use the correct specifier in my code, it works fine:
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main (String[] args) {
String format = "EEE, dd MMM yyyy HH:mm:ss z";
String date = "Fri, 12 Sep 2014 05:00:23 GMT";
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
Date dt = sdf.parse(date);
System.out.println(dt);
} catch (Exception e) {
e.printStackTrace();
}
}
}
The output of that program (for Perth, Western Australia, which is UTC+8) is, as expected:
Fri Sep 12 13:00:23 WST 2014
See the SimpleDateFormat online documentation for more detail.
If you still have troubles, try removing the individual items from your format and date strings until it starts working. Once you've established the problematic format specifier, it will be easier to track down.
Use "z" in your pattern because GMT is not just a literal but has to be interpreted as timezone offset UTC+00:00. And set the locale to English because you have english abbreviations of day-of-week and month:
String format = "EEE, dd MMM yyyy HH:mm:ss z";
String date = "Fri, 12 Sep 2014 05:00:23 GMT";
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.ENGLISH);
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 need to transform a date from this format : Tue Dec 10 09:00:00 EET 2013 into 3 12 2013 09:00
I've tried something like:
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm");
Date newdata = dateFormat.parse(pubDate);
SimpleDateFormat desiredDateFormat = new SimpleDateFormat("dd-MM-yyyy, hh:mm");
this.pubDate = desiredDateFormat.parse(desiredDateFormat.format(newdata));
but the date is not changing. How I can accomplish this?
Thanks!
This simple line will solve your problem
android.text.format.DateFormat dateFormat= new android.text.format.DateFormat();
dateFormat.format("dd-MM-yyyy hh:mm", new java.util.Date());