How to print UTC or GMT using SimpleDateFormat?

I am trying to print a date in this format: 2023-01-11 09:25:52 UTC But when I use date format: yyyy-MM-dd HH:mm:ss Z I get: 2023-01-11 09:29:25 +0100 >Solution : Use a lowercase z instead of Z to get the offset instead of the id. And you have to set the time zone using simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")). Example:… Read More How to print UTC or GMT using SimpleDateFormat?

Error for formatting Cannot format given Object as a Date

I have this code: private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd’T’HH:mm:ss.SSSZ"); Now, I try to format check some String data, final String format = SIMPLE_DATE_FORMAT.format("2022-12-07T10:57:23.970Z"); I get the error that Cannot format the given Object as a Date. Whats the issue here and how do I resolve it? >Solution : There are two issues… Read More Error for formatting Cannot format given Object as a Date

Java SimpleDateFormat Cannot format given Object as a Date

I am trying to convert string to date using date format and used the following code but its shows an error. public static Date ConvertStringtodate(String Date) throws ParseException { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date Teststart = dateFormat.parse(Date); return Teststart; } public static void main(String[]agrs) throws ParseException { System.out.println(ConvertStringtodate("2022.02.10 17:54:55")); } Here is the… Read More Java SimpleDateFormat Cannot format given Object as a Date