1
public class DatePgm {
    public static void main(String[] args) throws ParseException {
        
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'+'SS:SZ");
        Date date1 = sdf.parse("2020-07-26T18:52:24+05:30");
        System.out.println("DATE="+sdf.format(date1));
    }
}

Can any one help me to print the exact date and time format "2020-07-26T18:52:24+05:30"

Turo
  • 4,724
  • 2
  • 14
  • 27
  • 1
    Why dont you try to use LocalDateTime, which came with java 8. SimpleDateFormat is deprecated now. – Saurabh Jhunjhunwala Jul 31 '20 at 07:43
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead just use `OffsetDateTime` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jul 31 '20 at 18:39
  • Does this answer your question? [Java SimpleDateFormat Timezone offset with minute separated by colon](https://stackoverflow.com/questions/15245307/java-simpledateformat-timezone-offset-with-minute-separated-by-colon). I’m immodest enough to recommend [my own answer here](https://stackoverflow.com/a/43895243/5772882). – Ole V.V. Jul 31 '20 at 18:41

6 Answers6

2

tl;dr ⇒ java.time.OffsetDateTime

In this case, you don't really need to define a format/formatter yourself. The default DateTimeFormatter of an OffsetDateTime (which appears suitable here) is able to parse (and print/format) your example String. You can do it as follows:

public static void main(String[] args) {
    // your example datetime String
    String datetime = "2020-07-26T18:52:24+05:30";
    // parse the String to an OffsetDateTime using the default formatter
    OffsetDateTime odt = OffsetDateTime.parse(datetime);
    // and print the OffsetDateTime using its default formatter
    System.out.println("DATE=" + odt);
}

the output is

DATE=2020-07-26T18:52:24+05:30
deHaar
  • 17,687
  • 10
  • 38
  • 51
1

Use Instant instead of Date. Instant supports date time with zone

Instant date1 = Instant.parse("2020-07-26T18:52:24+05:30");
System.out.println("DATE= " + date1);
Thirumal
  • 8,280
  • 11
  • 53
  • 103
  • Correct. But you should add a note that using `Instant` automatically adjusts to UTC, an offset of zero hours-minutes-seconds. If the original offset is desirable, use `OffsetDateTime` instead. – Basil Bourque Jul 31 '20 at 19:47
1

You can try following,

String string = "2020-07-26T18:52:24+05:30";
    OffsetDateTime odt = OffsetDateTime.parse( string );
    System.out.println(odt);
  • 1
    Your code is just fine, but maybe answering a different question. i at least read this question as wanting to *print* rather than *parse* the format shown (it’s ISO 8601). Also some explanation along with your code would help. – Ole V.V. Jul 31 '20 at 18:46
0

You can use Calendar to get the date:

String time = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'+'SS:SZ").format(Calendar.getInstance().getTime());
Cardstdani
  • 4,999
  • 3
  • 12
  • 31
  • 1
    The time in my time zone is 2020-07-31T20:42:40.311, and I’m at offset +02:00 from UTC. Your code gave me `2020-07-31T20:42:40+311:311+0200`. It’s not quite right. And we should neither use `SimpleDateFormat` nor `Calendar`, both are poorly designed and both are long outdated. – Ole V.V. Jul 31 '20 at 18:44
  • 1
    FYI, the terribly flawed date-time classes such as [`java.util.Date`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Calendar.html), `GregorianCalendar`, and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/tutorial/datetime/TOC.html) classes built into Java 8 and later. – Basil Bourque Jul 31 '20 at 19:47
0

I think you should use LocalDatetIme, from java 8. A very good example for most of the formats are available here

Saurabh Jhunjhunwala
  • 2,832
  • 3
  • 29
  • 57
  • `LocalDateTime` is the wrong type for this input. The `LocalDateTime` class lacks any concept of time zone or offset. The `+05:30` in an offset-from-UTC of five and a half hours ahead of UTC. So the appropriate class here is `OffsetDateTime`. See [correct Answer by deHaar](https://stackoverflow.com/a/63193221/642706). – Basil Bourque Jul 31 '20 at 19:44
0

With SimpleDateFormat you have to set the timzone(Asia/Colombo is +05:30):

  SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
  Date date1 = sdf.parse("2020-07-26T18:52:24+05:30");
  sdf.setTimeZone(TimeZone.getTimeZone("Asia/Colombo"));
  System.out.println("DATE="+sdf.format(date1));

Output:

DATE=2020-07-26T18:52:24+05:30

Turo
  • 4,724
  • 2
  • 14
  • 27
  • 1
    FYI, the terribly flawed date-time classes such as [`java.util.Date`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Calendar.html), `GregorianCalendar`, and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/tutorial/datetime/TOC.html) classes built into Java 8 and later. – Basil Bourque Jul 31 '20 at 19:42