How to convert local timestamp to UTC in Java?
The below code can be used to convert the local timestamp to UTC in Java. Converting to any other timezone is very easy by changing the below UTC into the target timezone.
public static String getCurrentUTCTimestamp() {
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
final TimeZone utc = TimeZone.getTimeZone("UTC");
dateFormatter.setTimeZone(utc);
return dateFormatter.format(new java.util.Date());
}
Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

Comments
No comments yet.
Leave a comment