SimpleDateFormat in UTC to String in given format

date, datetime, java, simpledateformat, time

Solution

You can either use this:

final SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yy hh.mm.ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
final String utcTime = sdf.format(new Date());

or have a look at java8 LocalDateTime and DateTimeFormat API. java.util.Date and especially SimpleDateFormat are kinda outdated and I would recommend against using SimpleDateFormat if possible as it is not thread save which may bite you when using streams or something.

Java 8 alternative:

String now = ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ofPattern("dd-MMM-yy HH.mm.ss"));

Problem

I need add something to this line: ``` String date = new SimpleDateFormat("dd-MMM-yy hh.mm.ss").format(new Date()).toUpperCase(); ``` because I need time from UTC, not from my current location. Could you please help me?

Original source