1

I'm using java's SimpleDateFormat, here is my code: Date date = new Date(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ"); String strDate = simpleDateFormat.format(date); System.out.println(strDate);

Which print out:

2019-11-15T11:59:47.289+0200

But, I want to have a colon inside the offset, which means it need to look like this:

2019-11-15T11:59:47.289+02:00

Is there a way to adding a time zone that printed out like the second example here?

Moshe9362
  • 352
  • 2
  • 15
  • 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. Nov 15 '19 at 19:19

4 Answers4

4

What you are talking about is not a time zone (like UTC), it is an offset (like +01:00).

You can use the modern date time API java.time, which has a built-in DateTimeFormatter.ISO_OFFSET_DATE_TIME, that formats the offset as desired:

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;


public class StackoverflowDemo {

    public static void main(String[] args) {
        OffsetDateTime odt = OffsetDateTime.now();
        System.out.println(odt.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
    }
}

The output on my system is this:

2019-11-15T11:30:46.532+01:00
deHaar
  • 17,687
  • 10
  • 38
  • 51
  • Thanks, fixed my question. Also, accept this answer as, in my opinion, it much more simple and readable then using XXX as suggested below. – Moshe9362 Nov 15 '19 at 10:52
  • 2
    @Moshe9362 Thanks for accepting, I'm glad it helped. Consider using `java.time` for all operations on dates, times, calendary, time zones, offsets and so on. Don't write new programs using `java.util.Date`, that's legacy code. – deHaar Nov 15 '19 at 10:54
1

this snippet below gives result like this

2019-11-15T16:03:53+05:30

SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
System.out.println(dateFormat2.format(new Date()));

hope this is what you are looking for

Madhu Nair
  • 428
  • 1
  • 7
  • 20
1

Your format should be yyyy-MM-dd'T'hh:mm:ss.SSSXXX

Udith Gunaratna
  • 2,091
  • 1
  • 13
  • 17
0

I think your best bet is to use DateTimeFormatter.

You can find documentation here: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

Martin Frank
  • 3,445
  • 1
  • 27
  • 47
  • 1
    Hey Konrad, thank you for answering, a code sample would be nice to complete your answer – Matt Nov 15 '19 at 11:20
  • Welcome to Stack Overflow and thanks for wanting to contribute. Your answer isn’t really much more than a link. Such isn’t really helpful according to experience. Please include some essence from the linked page in your answer. You can always edit your answer if you want to improve it. I agree that it’s best to use `DateTimeFormatter`. – Ole V.V. Nov 15 '19 at 19:14