0

I get response from an News API detailing the time an article was published as a UTC string ex below:

"2020-07-17T19:30:40Z"

The goal is to have the articles say when they were published in hours ago for example, a news article could say that it was published 5 hours ago. I created function to convert the time string to a locale time format, although it doesn't work as intended at all. Below is the function:

public static String convertUtc2Local(String utcTime) {
    String convertedTime = utcTime;
    String pattern = "MMMM d, yyyy";
    Locale locale = Locale.getDefault();
    /*TODO Fix this it just shows todays date not the actual date string*/
    Date date = new Date();


    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern, locale);
    convertedTime = simpleDateFormat.format(date);
    return convertedTime;

}

How do I convert the UTC time string to hours ago?

JavaHava
  • 189
  • 2
  • 15

1 Answers1

0

// Convert UTC to Local Time

Date fromGmt = new Date(gmtTime.getTime() + TimeZone.getDefault().getOffset(localTime.getTime()));
System.out.println("UTC time:" + gmtTime.toString() + "," + gmtTime.getTime() + " --> Local:"
        + fromGmt.toString() + "-" + fromGmt.getTime());
Jerry
  • 366
  • 4
  • 22