I´m having a stupid problem with java.util.Date.
I have this line of code, but I don´t understand why this date is unparseable with this format.
public class TestTime {
public static void main(String[] args) {
final String DATE_FORMAT = "EEE MMM dd HH:mm:ss zzz yyyy";
String date = "Sat Dec 31 10:00:00 CET 2011";
SimpleDateFormat dFormat = new SimpleDateFormat(DATE_FORMAT);
Date lDate = null;
try {
lDate = dFormat.parse(date);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
If your system uses a locale other than English you need to use this constructor:
SimpleDateFormat(DATE_FORMAT,Locale.ENGLISH);
If this is not the problem, you should format a date using the same formatter and compare the output to your input string.
I don't see anything wrong with this. It executes for me without error, and returns:
Sat Dec 31 09:00:00 GMT 2011
Seems to be a Locale-related problem.
If I set a French locale, the pattern does not work. If I set the Locale to be US in the SimpleDateFormat constructor, it does works.
SimpleDateFormat dFormat = new SimpleDateFormat(DATE_FORMAT, Locale.US);
Related
I have given text field expression new.java.util.Date() and pattern MMMMM dd, yyyy as the mentioned format.
The date must display like: jan 13, 2012 but it's displaying in some other format: Fri Jan 13 08:30:12 IST 2012.
So how to print the date in the mentioned format. And one thing in preview the date displays correctly as mentioned but inside my application it displays Fri Jan 13 08:30:12 IST 2012 format. Is there any way to make it to work properly?
new SimpleDateFormat("MMM dd, yyyy ").format(new Date())
Put the above line in text field so you will get your Date format
Use this below method..hope it will help to you
public static String getDateTimeForUgcServer(String date)
{
SimpleDateFormat intputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dt = new Date();
try
{
dt = intputFormat.parse(date);
}
catch (ParseException e)
{
e.printStackTrace();
}
SimpleDateFormat outputFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String str = outputFormat.format(dt);
return str;
}
I'm having a tough time parsing this date its the +0 at the end that is causing a problem, does anyone know whats wrong with my format string?? If I remove the +0 from the date string and the Z from the format string it works fine, unfortunately for my application that isn't an option.
public class Main {
/**
* #param args
*/
public static void main(String[] args) {
SimpleDateFormat dateFormater = new SimpleDateFormat("E, dd MMM yyyy kk:mm:ss zZ");
try {
Date d = dateFormater.parse("Sun, 04 Dec 2011 18:40:22 GMT+0");
System.out.println(d.toLocaleString());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
One approach is to use normal string-manipulation techniques to translate your string from a form that you're expecting to a form that SimpleDateFormat will understand. You haven't said exactly what range of time-zone formats are acceptable, but one possibility is something like this:
private static Date parse(String dateString) throws ParseException
{
final SimpleDateFormat dateFormat =
new SimpleDateFormat("E, dd MMM yyyy kk:mm:ss Z");
dateString = dateString.replaceAll("(GMT[+-])(\\d)$", "$1\\0$2");
dateString = dateString.replaceAll("(GMT[+-]\\d\\d)$", "$1:00");
return dateFormat.parse(dateString);
}
That would support GMT plus-or-minus a one-or-two-digit hour offset, in addition to still supporting anything already supported by SimpleDateFormat, such as EST or GMT+1030.
Alternatively, if you know it will always be GMT, then you can just set the time-zone on the formatter, and ignore the time-zone in the string:
private static Date parse(String dateString) throws ParseException
{
final SimpleDateFormat dateFormat =
new SimpleDateFormat("E, dd MMM yyyy kk:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
return dateFormat.parse(dateString);
}
You can also split the difference. I notice that the time-zone format in your string matches what's expected by TimeZone.getTimeZone(). Is that intentional? If so, you can grab that time-zone format out of the string, pass it to dateFormat.setTimeZone beforehand, and then ignore it during actual parsing:
private static Date parse(final String dateString) throws ParseException
{
final SimpleDateFormat dateFormat =
new SimpleDateFormat("E, dd MMM yyyy kk:mm:ss");
if(dateString.indexOf("GMT") > 0)
dateFormat.setTimeZone
(
TimeZone.getTimeZone
(dateString.substring(dateString.indexOf("GMT")))
);
return dateFormat.parse(dateString);
}
If the format is that consistent, you could append 0:00 to the date string.
String dateString = "Sun, 04 Dec 2011 18:40:22 GMT+0";
SimpleDateFormat sdf = new SimpleDateFormat("E, dd MMM yyyy kk:mm:ss z", Locale.ENGLISH);
Date date = sdf.parse(dateString + "0:00");
System.out.println(date);
(note that I fixed the SimpleDateFormat construction to explicitly specify the locale which would be used to parse the day of week and month names, otherwise it may fail on platforms which does not use English as default locale; I also wonder if you don't actually need HH instead of kk, but that aside)
I am trying to add a new pattern to the date display but I am not getting the result that I am expecting:
Here is my code:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S");
sdf.applyPattern("yyyy-MM-dd");
Date date_out = null;
try {
date_out = sdf.parse(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
((TextView) view.findViewById(R.id.date)).setText(date_out.toString());
I want the output to look something like this: 03 Oct 2011
However this is the output tat I am getting: Oct 03 00:00:00 GMT+ 11:00 2011
How do I reach the desired output?
EDIT:
I solved this code by adding this line:
sdf.format(date_out);
instead of setText()
Date.toString(); does always format your String that way. You should a SimpleDateFormat to format the Date object to the String you want.
The JavaDoc of the Date.toString(); method says:
Converts this Date object to a String of the form:
dow mon dd hh:mm:ss zzz yyyy
You have to use two SimpleDateFormat objects. One for parsing the input and an other one for formatting the parsed Date object to String.
final String inputDate = "2011-05-08T11:12:13.0123";
final SimpleDateFormat inputParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S");
Date date_out = null;
try {
date_out = inputParser.parse(inputDate);
} catch (final ParseException e) {
e.printStackTrace();
}
final SimpleDateFormat outputFormatter =
new SimpleDateFormat("dd MMM yyyy", Locale.US);
final String result = outputFormatter.format(date_out);
System.out.println(result);
Output:
08 May 2011
The Date.toString() method formats the string like that (check the api documentation).
You do not actually need to use applyFormat(...) in this case. You want to parse one format and output it in another format.
To parse the date (given the string: 2011-10-03") use can use the format"yyyy-MM-dd"and when you output theDateyou want to use"dd MMM yyyy"`:
public static void main(String[] args) throws ParseException {
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy MMM dd");
Date parsedDate = inputFormat.parse("2011-10-05");
System.out.println(outputFormat.format(parsedDate));
}
Outputs (on US locale):
2011 Oct 05
Read below document :-
http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
hope help u above link.
This question is a duplicate of this question by intention. It seems like the older one is "ignored", while none of the answers there is the answer.
I need to parse a given date with a given date pattern/format.
I have this code, which is supposed to work:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main
{
public static void main(String[] args)
{
final Date date = string_to_date("EEE MMM dd HH:mm:ss zzz yyyy",
"Thu Aug 14 16:45:37 UTC 2011");
System.out.println(date);
}
private static Date string_to_date(final String date_format,
final String textual_date)
{
Date ret = null;
final SimpleDateFormat date_formatter = new SimpleDateFormat(
date_format);
try
{
ret = date_formatter.parse(textual_date);
}
catch (ParseException e)
{
e.printStackTrace();
}
return ret;
}
}
For some reason I'm getting this output:
java.text.ParseException: Unparseable date: "Thu Aug 14 16:45:37 UTC 2011"
at java.text.DateFormat.parse(DateFormat.java:337)
at Main.string_to_date(Main.java:24)
at Main.main(Main.java:10)
null
What's wrong with my date pattern? This seems to be a mystery.
Your platform default locale is apparently not English. Thu and Aug are English. You need to explicitly specify the Locale as 2nd argument in the constructor of SimpleDateFormat:
final SimpleDateFormat date_formatter =
new SimpleDateFormat(date_format, Locale.ENGLISH); // <--- Look, with locale.
Without it, the platform default locale will be used instead to parse day/month names. You can learn about your platform default locale by Locale#getDefault() the following way:
System.out.println(Locale.getDefault());
This should parse the given string.
public static void main(String[] args) throws ParseException
{
String sd = "Thu Aug 14 16:45:37 UTC 2011";
String dp = "EEE MMM dd HH:mm:ss zzz yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(dp);
Date d = sdf.parse(sd);
System.out.println(d);
}
I should have specify a locale, like this:
final SimpleDateFormat date_formatter = new SimpleDateFormat(date_format, Locale.ENGLISH);
Thanks to BalusC with his great answer!
I want to convert the date string in a Twitter response to a Date object, but I always get a ParseException and I cannot see the error!?!
Input string: Thu Dec 23 18:26:07 +0000 2010
SimpleDateFormat Pattern:
EEE MMM dd HH:mm:ss ZZZZZ yyyy
Method:
public static Date getTwitterDate(String date) {
SimpleDateFormat sf = new SimpleDateFormat(TWITTER);
sf.setLenient(true);
Date twitterDate = null;
try {
twitterDate = sf.parse(date);
} catch (Exception e) {}
return twitterDate;
}
I also tried this: http://friendpaste.com/2IaKdlT3Zat4ANwdAhxAmZ but that gives the same result.
I use Java 1.6 on Mac OS X.
Cheers,
Andi
Your format string works for me, see:
public static Date getTwitterDate(String date) throws ParseException {
final String TWITTER="EEE MMM dd HH:mm:ss ZZZZZ yyyy";
SimpleDateFormat sf = new SimpleDateFormat(TWITTER);
sf.setLenient(true);
return sf.parse(date);
}
public static void main (String[] args) throws java.lang.Exception
{
System.out.println(getTwitterDate("Thu Dec 3 18:26:07 +0000 2010"));
}
Output:
Fri Dec 03 18:26:07 GMT 2010
UPDATE
Roland Illig is right: SimpleDateFormat is Locale dependent, so
just use an explicit english Locale:
SimpleDateFormat sf = new SimpleDateFormat(TWITTER,Locale.ENGLISH);
This works for me ;)
public static Date getTwitterDate(String date) throws ParseException
{
final String TWITTER = "EEE, dd MMM yyyy HH:mm:ss Z";
SimpleDateFormat sf = new SimpleDateFormat(TWITTER, Locale.ENGLISH);
sf.setLenient(true);
return sf.parse(date);
}
Maybe you are in a locale where ‘Tue‘ is not a recognized day of week, for example German. Try to use the ‘SimpleDateFormat‘ constructor that accepts a ‘Locale‘ as a parameter, and pass it ‘Locale.ROOT‘.
You should not have ZZZZZ but only Z for the timezone.
See samples in http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html for more information.
EEE, d MMM yyyy HH:mm:ss Z > Wed, 4 Jul 2001 12:08:56 -0700
SimpleDateFormat is not thread safe. "EEE MMM dd HH:mm:ss ZZZZZ yyyy" was working in our application, but failing in a small percentage of cases. We finally realized that the issue was coming from multiple threads using the same instance of SimpleDateFormat.
Here is one workaround: http://www.codefutures.com/weblog/andygrove/2007/10/simpledateformat-and-thread-safety.html
Function for convert Twitter Date :
String old_date="Thu Jul 05 22:15:04 GMT+05:30 2012";
private String Convert_Twitter_Date(String old_date) {
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss");
SimpleDateFormat old = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZZZ yyyy",Locale.ENGLISH);
old.setLenient(true);
Date date = null;
try {
date = old.parse(old_date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sdf.format(date);
}
The output format like : 05-Jul-2012 11:54:30