0

I am having string date in this formate (yyyy-MM-dd HH:mm:ss.SSS) how to convert this to UTC timestamp.

2 Answers2

0

please try like this :


        long time = 0;
        try {
            Date date1 = new Date((new Date()).getTime());
            SimpleDateFormat formatNowDay = new SimpleDateFormat("dd", Locale.getDefault());
            SimpleDateFormat formatNowMonth = new SimpleDateFormat("MM", Locale.getDefault());
            SimpleDateFormat formatNowHours = new SimpleDateFormat("HH", Locale.getDefault());
            SimpleDateFormat formatNowMinute = new SimpleDateFormat("mm", Locale.getDefault());
            SimpleDateFormat formatNowSecond = new SimpleDateFormat("ss", Locale.getDefault());
            SimpleDateFormat formatGMT = new SimpleDateFormat("zzz", Locale.getDefault());

            String currentDay = formatNowDay.format(date1);
            String currentMonth = formatNowMonth.format(date1);
            String currentHours = formatNowHours.format(date1);
            String currentMinute = formatNowMinute.format(date1);
            String currentSecond = formatNowSecond.format(date1);
            String GMT = formatGMT.format(date1);

            String str_date = currentDay + currentMonth + "1970" + currentHours + currentMinute + currentSecond + GMT;
            DateFormat formatter = new SimpleDateFormat("ddMMyyyyHHmmsszzz", Locale.getDefault());
            Date date = formatter.parse(str_date);
            time = (date.getTime() / 1000L);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return String.valueOf(time);
    }
mohammad
  • 9
  • 2
0

The format you have mentioned does not have the timezone information. Therefore, first, you should parse it to a LocalDateTime using a DateTimeFormatter and then convert this LocalDateTime for ZoneOffset.UTC to an Instant.

Demo:

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        // A sample date-time string
        String dateStr = "2020-02-29 19:56:36.234";
        
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS", Locale.ENGLISH);     
        LocalDateTime ldt = LocalDateTime.parse(dateStr, dtf);
        
        Instant instant = ldt.toInstant(ZoneOffset.UTC);
        System.out.println(instant);
    }
}

Output:

2020-02-29T19:56:36.234Z

ONLINE DEMO

An Instant represents an instantaneous point on the timeline in UTC. The Z in the output is the timezone designator for a zero-timezone offset. It stands for Zulu and specifies the Etc/UTC timezone (which has the timezone offset of +00:00 hours).

Note: The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*. For any reason, if you need to convert this object of Instant to an object of java.util.Date, you can do so as follows:

Date date = Date.from(instant);

Learn more about the modern Date-Time API from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110