nidhal belhadj salem
nidhal belhadj salem

Reputation: 35

How to convert "2019-02-10T19:30:00+00:00 " date format to "19:30"

I am a new android developer. I would like to parse this date: 2019-02-10T19:30:00+00:00 to this format 19:30 .

Upvotes: 0

Views: 494

Answers (4)

secret paladin
secret paladin

Reputation: 222

try this in kotlin:

 <your_date>.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT))

example output is

2:11 PM

Upvotes: 0

You can use native SimpleDateFromat to parse such dates.

For example:

String yourTime = "2019-02-10T19:30:00+00:00";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD'T'hh:mm:ss", Locale.getDefault());
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
    try {
        calendar.setTime(sdf.parse(yourTime));
    } catch (ParseException e) {
        e.printStackTrace();
    }
    SimpleDateFormat output = new SimpleDateFormat("HH:mm", Locale.getDefault());
    System.out.println(output.format(calendar.getTime()));

Upvotes: 0

Luca Murra
Luca Murra

Reputation: 1892

If you're sure the format of your string would be xxxx-xx-xxxAB:CD:xx+xx:xx and you need AB:CD you can do this:

val stringToFormat = "2019-02-10T19:30:00+00:00"
val formattedString = stringToFormat.substring(11,16)

But the question should be more precise...

Upvotes: 0

Selin Kaplan
Selin Kaplan

Reputation: 28

SimpleDateFormat dateFormat = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss.SSS”);
Date sourceDate = null;
try {
sourceDate = dateFormat.parse(sourcedatevalue);
} catch (ParseException e) {
e.printStackTrace();
}

SimpleDateFormat targetFormat = new SimpleDateFormat(“HH:mm”);
targetdatevalue= targetFormat.format(sourceDate);

You can use this template.

Upvotes: 1

Related Questions