Get only the time of a TimeStamp (JAVA / Android)

0

I would like to know how I can get only the time of a TimeStamp like the following:

From here I suppose that if I only take the time I should be given 20:00 PM (I do not want seconds).

I have also read online that people prefer to use Joda Time on the tools that Java already offers on their jdk.

How could I extract that information?

    
asked by TwoDent 13.10.2018 в 00:41
source

1 answer

3

Using SimpleDateFormat and the appropriate pattern, in your case < strong> yyyy-MM-dd'T'HH: mm: ssZ so that with the parse method of DateFormat convert the java.lang.String to java.util.Date and then the suitable pattern HH: mm (Time of day (0-23)) or h: mm to (Time am / pm (1-12)), to convert the < em> java.util.Date in java.lang.String :

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class Test {

    public static void main(String[] args) {

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
        Date result;
        try {
            //Aqui se convierte en Date
            result = df.parse("2018-11-20T20:00:00+0000");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
            System.out.println(sdf.format(result));
            sdf = new SimpleDateFormat("HH:mm");
            sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
            System.out.println(sdf.format(result));
            sdf = new SimpleDateFormat("h:mm a");
            sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
            System.out.println(sdf.format(result));


        }catch(Exception e){
            e.printStackTrace();
        }
    }

}

This is the result:

2018-11-20 20:00:00

20:00

8:00 PM

    
answered by 13.10.2018 / 02:20
source