Why does the following give an error:
DateFormat formatter1 = new SimpleDateFormat("E, MMM d");
formatter1.setTimeZone(TimeZone.getTimeZone("America/New_York"));
formatter1.parse("Tue, Nov 26");
I don't get why it isn't working.
You should set a Locale to your formatter where months are spelt in English, otherwise it's using your default Locale :
SimpleDateFormat(String pattern)
Constructs a SimpleDateFormat using the given pattern and the default
date format symbols for the default locale.
I.e :
DateFormat formatter1 = new SimpleDateFormat("E, MMM d", Locale.US);
Related
I am trying to convert date string from one format to another using SimpleDateFormat.
Conversion works but there is a dot "." after month.
String dateStr = "04/02/1987";
DateFormat df1 = new SimpleDateFormat("dd/MM/yyyy");
Date d = df1.parse(dateStr);
DateFormat df2 = new SimpleDateFormat("dd MMM yyyy");
System.out.println(df2.format(d));
Output is 04 Feb. 1987 instead of 04 Feb 1987.
What is your Locale.getDefault()?
Different output for alphanumeric date parts may be caused by the Locale the formatter is using. In most cases, the system default Locale is used if you don't specify one yourself. I don't know for sure SimpleDateFormat does so, but it seems likely.
I know that a java.time.format.DateTimeFormatter does so, see the following example which uses java.time, the modern and recommended to use datetime API:
public static void main(String[] args) {
String dateStr = "04/02/1987";
LocalDate localDate = LocalDate.parse(dateStr, DateTimeFormatter.ofPattern("dd/MM/yyyy"));
System.out.println(localDate.format(DateTimeFormatter.ofPattern("dd MMM yyyy",
Locale.ENGLISH)));
System.out.println(localDate.format(DateTimeFormatter.ofPattern("dd MMM yyyy",
Locale.FRENCH)));
}
Output:
04 Feb 1987
04 févr. 1987
The output is (of course) different concerning the name of the month, but using Locale.FRENCH shows a dot after the abbreviated month name. It is possible that your system's default Locale is one that indicates an abbreviation by a dot, too, but is identical to the output format of a Locale.ENGLISH for the numeric parts and the abbreviation of the month.
Please provide Locale.ENGLISH in SimpleDateFormat constructor while creating object as shown below:
String dateStr = "04/02/1987";
DateFormat df1 = new SimpleDateFormat("dd/MM/yyyy");
Date d = df1.parse(dateStr);
DateFormat df2 = new SimpleDateFormat("dd MMM yyyy", Locale.ENGLISH);
System.out.println(df2.format(d));
I am getting dates in this format 14-MAY-13 01.17.16.250000 PM and
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yy hh:mm:ss aaa");
simpleDateFormat.parse("14-MAY-13 01.17.16.250000 PM")
Gives me this exception
java.text.ParseException: Unparseable date: "14-MAY-13 01.17.16.250000 PM" ..What should be format of simpledateformat?
Try
new SimpleDateFormat("dd-MMM-yy hh.mm.ss.SSSSSS aaa");
The format must be corrected (see the javadoc for more information). You should also set the Locale to the SimpleDateFormat to be sure the month will be parsed with the correct language.
new SimpleDateFormat("dd-MMM-yy hh.mm.ss.SSSSSS aaa", Locale.US);
If you don't give the Locale, SimpleDateFormat will use the default locale of your system which can be not the locale of the given string input.
You need to use the SSSSSS for milliseconds and replace : with . to match this part 01.17.16.250000.
SimpleDateFormat simpleDateFormat =
new SimpleDateFormat("dd-MMM-yy hh.mm.ss.SSSSSS aaa");
In your Formatstring you expect hh:mm:ss but in your input string your have hh.mm.ss and you also didn't include the miliseconds.
So the format string should be dd-MMM-yy hh.mm.ss.SSSSSS aaa
Simple date format and date formate should be same, try this
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yy hh:mm:ss:SSSSSS aaa");
Date d = simpleDateFormat.parse("14-MAY-13 01:17:16:250000 PM");
I am getting parser Exception while parsing the below date using simple date format API.
String inputTimeStamp = "2012/07/19 09:49:00 - GMT -08:00";
java.text.SimpleDateFormat dateformate= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
effDate = dateformate.parse(inputTimeStamp);
Please help me out on this.
Change
java.text.SimpleDateFormat dateformate=
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
to
java.text.SimpleDateFormat dateformate=
new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
You have slashes (/) in your inputTimeStamp
Because you are parsing a date which is in a different format then you have described it to the SimpleDateFormat. If effDate is of type String and should hold the formatted date, the following code might solve it, although there is not supposed to be an space between GMTand -08:00 .
String inputTimeStamp = "2012/07/19 09:49:00 - GMT -08:00";
SimpleDateFormat inputDateformat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss - z");
SimpleDateFormat dateformate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
effDate = dateformate.format(inputDateFormat.parse(inputTimeStamp));
I would highly recommend also joda time, especially when you want to do calculations on the date.
String inputTimeStamp = "2012/07/19 09:49:00 - GMT -08:00";
DateTimeFormatter inputDateformat = DateTimeFormat.forPattern("yyyy/MM/dd HH:mm:ss - z Z");
DateTimeFormatter dateformate = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
effDate = dateformate.print(inputDateFormat.parseDateTime(inputTimeStamp));
The pattern would be: yyyy/MM/dd HH:mm:ss - z where z is for General Time Zone. SimpleDateFormat.
Try this:
new SimpleDateFormat("yyyy/MM/dd HH:mm:ss - z");
See this FYR
I try to parse a date in the format YYYY-MM-DD HH:mm:ss
String now = "2012-11-02 12:02:00";
DateFormat formatter;
formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date date_temp = (Date) formatter.parse(now.toString());
System.out.println("output: " + date_temp);
It gives me following exception
java.text.ParseException: Unparseable date: "2012-11-02 12:02:00"
Well yes, you've created a formatted with one format ("EEE MMM dd HH:mm:ss z yyyy") and then given it a string in a completely different format to parse. Why did you think that would work? Try this:
// Locale specified to avoid any cultural differences. You may also
// want to specify the time zone.
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
Locale.US);
Date date = formatter.parse(now);
Note that the parsed Date does not know anything about formatting - the result of calling toString() (as you're doing implicitly here) is always just the default format, in the JRE default time zone. If you want to print it out with a particular format, use SimpleDateFormat again.
Also note that I've combined declaration and initialization for the variable. Prefer that over declaring a variable in one line and giving it an initial value later.
Of course your date string is not in the format you are using in SimpleDateFormat. So it won't be able to parse it into a date object.
Try using this: -
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
How can I convert dates with full month names into a Date object in Java? Here's a sample date that I need to convert: 6 December 2002.
Thanks.
String str = "6 December 2002";
DateFormat formatter = new SimpleDateFormat("dd MMM yyyy");
Date date = (Date)formatter.parse(str);
Here is working IDE One demo
Must See
API Doc
Have a look at SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("d MMMMM yyyy", Locale.US);
Date d = sdf.parse("6 December 2002");
System.out.println(d);