1

I have this value,

2011-11-19T00:00:00.000-03:00

which needs to be converted and set it in this format

yyyyMMdd

I tried this way

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class ParsedDate {

    public static void main(String args[]) throws ParseException {
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ",
                Locale.US);
        String s1 = "2011-11-19T00:00:00.000-03:00";
        Date d = sdf.parse(s1);
        String s2 = (new SimpleDateFormat("yyyyMMdd")).format(d);
        System.out.println(s2);

    }
}

But it is giving me

Exception in thread "main" java.text.ParseException: Unparseable date: "2011-11-19T00:00:00.000-03:00"

Could anybody please help me?

Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
  • 5
    possible duplicate of [SimpleDateFormat Unparseable date Exception](http://stackoverflow.com/questions/7681782/simpledateformat-unparseable-date-exception) - took me about 30 seconds of clicking those links on the right to find an exact match to your pattern. – Brian Roach Feb 10 '12 at 06:49
  • +1 for having a concise compiling example – Urs Reupke Feb 10 '12 at 06:49
  • @BrianRoach, quite certainly is. – Urs Reupke Feb 10 '12 at 06:50

2 Answers2

4

Try using this

 DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS",
                    Locale.US);
            String s1 = "2011-11-19T00:00:00.000-03:00";
            Date d;
            try
            {
                d = sdf.parse(s1);
                String s2 = (new SimpleDateFormat("yyyyMMdd")).format(d);
                System.out.println(s2);
            }
            catch (ParseException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

Notice that I have removed Z

Sandeep Nair
  • 3,630
  • 3
  • 26
  • 38
  • Thank you very much Sandeep Nair . –  Feb 10 '12 at 07:00
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque May 24 '18 at 06:20
1

tl;dr

OffsetDateTime.parse( "2011-11-19T00:00:00.000-03:00" )
.format(
    DateTimeFormatter.BASIC_ISO_DATE
)

20111119

java.time

The modern approach uses the java.time classes.

Parse your input as a OffsetDateTime object.

String input = "2011-11-19T00:00:00.000-03:00" ;
OffsetDateTime odt = OffsetDateTime.parse( input ) ;

Extract the date-only value care about, without a time-of-day and without a time zone.

LocalDate ld = odt.toLocalDate() ;

The DateTimeFormatter class offers a predefined object with your desired format, BASIC_ISO_DATE. This particular format is known officially as the “basic” variant of a standard ISO 8601 format, where 'basic' means minimizing the use of delimiters.

String output = ld.format( DateTimeFormatter.BASIC_ISO_DATE ) ;

20111119


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154