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.