-2

I have specific case that I must take the date field, convert it to GMT time and then to convert it to specific String format.

This gives the GMT time:

public static void main(String[] args) {        
    Date rightNow = Calendar.getInstance().getTime();
    DateFormat gmtFormat = new SimpleDateFormat();
    TimeZone gmtTime = TimeZone.getTimeZone("GMT");
    gmtFormat.setTimeZone(gmtTime);
    System.out.println("GMT Time: " + gmtFormat.format(rightNow));
    String gmtDate=gmtFormat.format(rightNow);
}

Now I need to that GMT time convert to String format yyyy-MM-ddTHH:mm:ssZ Example current time in my time zone is 17:10:00, in GMT 15:10:00 so it means final output should be 2017-08-07T15:10:00Z

I tried this code to add:

String pattern = "yyyy-MM-ddTHH:mm:ssZ";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String date = simpleDateFormat.format(gmtDate);
System.out.println(date);

But of course I am getting the exception because string cannot be converted like this, but I need something similar.

Veljko
  • 1,708
  • 12
  • 40
  • 80
  • 1
    Do you *have* to use the old date/time API? The java.time package is much, much nicer. Beyond that, it's not clear what exception you're getting or where... – Jon Skeet Aug 07 '17 at 15:07
  • Hi I am getting exception at this line: SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); this Exception in thread "main" java.lang.IllegalArgumentException: Illegal pattern character 'T' at java.text.SimpleDateFormat.compile(Unknown Source) at java.text.SimpleDateFormat.initialize(Unknown Source) – Veljko Aug 07 '17 at 15:11
  • 2
    Right, so have you investigated that? Basically your pattern is broken - you need to quote the `T` and the `Z`. – Jon Skeet Aug 07 '17 at 15:11
  • 2
    The correct pattern as shown with complete working examples in the duplicate is `public static final String ISO_8601_24H_FULL_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";` As per [ISO_8601#Combined_date_and_time_representations](http://en.wikipedia.org/wiki/ISO_8601#Combined_date_and_time_representations). Do **not** hard code the `Z` at the end as is suggested by others. –  Aug 07 '17 at 15:20

1 Answers1

-1

Merge your 2 codeblocks together:

public static void main(String[] args) {        
  Date rightNow = Calendar.getInstance().getTime();
  String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
  DateFormat gmtFormat = new SimpleDateFormat(pattern);
  TimeZone gmtTime = TimeZone.getTimeZone("GMT");
  gmtFormat.setTimeZone(gmtTime);
  System.out.println("GMT Time: " + gmtFormat.format(rightNow));
}

Or "yyyy-MM-dd'T'HH:mm:ss.SSSXXX" as per JavaDoc...

Usagi Miyamoto
  • 6,196
  • 1
  • 19
  • 33
  • this is not working: Exception in thread "main" java.lang.IllegalArgumentException: Illegal pattern character 'T' at java.text.SimpleDateFormat.compile(Unknown Source) at java.text.SimpleDateFormat.initialize(Unknown Source) at java.text.SimpleDateFormat.(Unknown Source) at java.text.SimpleDateFormat.(Unknown Source) at ibis.test.EricssonDate.main(EricssonDate.java:34) – Veljko Aug 07 '17 at 15:12
  • This is exactly the same exception which I am getting with my code also – Veljko Aug 07 '17 at 15:12
  • To be honest, i just copied your original pattern, did not looked it to be working. Corrected by @RobinTopper... – Usagi Miyamoto Aug 07 '17 at 15:18
  • no their edit was incorrect as well –  Aug 07 '17 at 15:18