1

Things I've tried

  public class Event {

  private SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddTHHmmssZ");
  
  private Date date;
  private String summary;
  
  public Date getDate() {
    return date;
  }
  
  public void setDate(Date date) {
    this.date = date;
  }
  
  public void setDate(String stringDate) throws ParseException {
    this.date = formatter.parse(stringDate);
  }
  
  public String getSummary() {
    return summary;
  }
  
  public void setSummary(String summary) {
    this.summary = summary;
  }
}

// main

String x = "20200926T221447Z"
Event event = new Event();
event.setDate(x);

Exception I get

Exception in thread "main" java.lang.IllegalArgumentException: Illegal pattern character 'T'
    at java.text.SimpleDateFormat.compile(SimpleDateFormat.java:826)
    at java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:634)
    at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:605)
    at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:580)
    at stuff.calendar.Event.<init>(Event.java:9)
    at stuff.calendar.App.main(App.java:34)

Question : Is my SimpleDateFormat incorrect? I've tried changing this around but none work. Is there another java API I should use to parse this particular time format?

JumbledCode
  • 85
  • 1
  • 7
  • 2
    You should be using the classes from the `java.time` package. `Date` is obsolete and most of its ilk are deprecated. – WJS Sep 27 '20 at 15:16
  • *Is there another java API I should use to parse this particular time format?* There certainly is, and all other formats too. @WJS said it already: [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Sep 27 '20 at 15:26

2 Answers2

4

When attempting to parse a String into a date-time object, I tend to prefer using a DateTimeFormatter over SimpleDateFormat.

I was able to parse your String into a ZonedDateTime object with the following snippet:

String x = "20200926T221447Z";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuuMMdd'T'HHmmssX");
System.out.println(ZonedDateTime.parse(x, dtf));

Output:

2020-09-26T22:14:47Z
Jacob G.
  • 28,856
  • 5
  • 62
  • 116
  • @Java4Life you should accept this answer if it meets your requirements. – WJS Sep 27 '20 at 15:17
  • Since I consider UTC (denoted by the trailing `Z`) rather an offset then a time zone, I’d prefer the `OffsetDateTime` class. Code will be the same otherwise. If the offset is required to be `Z` always, you may even use `Instant`, though the code is different: `dtf.parse(x, Instant::from)`. – Ole V.V. Sep 27 '20 at 15:31
  • @OleV.V. Thanks, I intended to use `Instant` in my solution, but couldn't figure out a way to easily parse it from a `DateTimeFormatter`. – Jacob G. Sep 27 '20 at 15:32
2

You have to put quotes around the string literals that should not be interpreted:

formatter = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");

Also consider that Z has a special parsing meaning (https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html). So if you actually want to include a timezone, you need to specify it in the input in RFC-822 format.

SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd'T'HHmmssZ");                                      
Date d = formatter.parse("20200926T221447-0800");
System.out.println(d);    

Use X instead if your timezone is ISO8601 like your example:

Example (which I think is what you want):

SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd'T'HHmmssX");
Date d = formatter.parse("20200926T221447Z");                                                                 
System.out.println(d);    
mprivat
  • 21,582
  • 4
  • 54
  • 64