0

I have below 2 date strings and I need to convert them into simple date strings.

String date1 = "2020-11-09T06:41:01-0800"; String date2 = "2020-11-09T17:01:15-0800";

I would like to convert them into below strings.

date1 = "6:41 am" date2 = "5:01 pm"

I tried various options in java and I could not get as expected. Can you please help me on this?

srini
  • 35
  • 3
  • Create a SimpleDateFormat instance, parse your Strings into Date objects, then use another SimpleDateFormat to output in the new format – ControlAltDel Nov 10 '20 at 05:32
  • please read this article about DateFormatting: https://www.javatpoint.com/java-string-to-date – Mustafa Poya Nov 10 '20 at 05:42
  • @ControlAltDel Please don’t.The `SimpleDateFormat` class is notoriously troublesome and long outdated. It’s much better to use [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Nov 10 '20 at 08:12
  • @MustafaPoya Please don’t. That page uses the `SimpleDateFormat` class which is notoriously troublesome and long outdated. It’s much better to use [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Nov 10 '20 at 08:13

3 Answers3

3

Try this one. I have used LocalDateTime which came to play after java 8. Since I have used the format patterns here, you can change this to run with any kind of date time patters by changing the formatter pattern.

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Test {

    public static void main(String[] args) {

        String date = "2020-11-09T06:41:01-0800";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ");
        LocalDateTime dateTime = LocalDateTime.parse(date, formatter);

        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("h:mm a");
        String formattedDateTime = dateTime.format(outputFormatter);
        System.out.println(formattedDateTime);
    }
}
Jagath01234
  • 139
  • 1
  • 10
  • 2
    I believe “-0800” is a zone offset, not “fraction of second”, if I am right, you might want to replace “-SSSS” with “Z”... – nkrivenko Nov 10 '20 at 06:45
  • 1
    Use single `h` rather then `hh` for the desired `6:41 AM` without leading zero. And give locale for the formatter since other languages than English use other texts for AM and PM. – Ole V.V. Nov 10 '20 at 08:26
  • Added the suggestions shown by nkrivenko and Ole V.V to make the answer more accurate.. – Jagath01234 Nov 11 '20 at 04:37
2

The zone offset for both the dates is 0800 which is CST – China Standard Time so AM/PM will be shown as 上午/下午. To prevent this, specify the default Locale using Locale.getDefault(), it returns the default locale set by the Java Virtual Machine. You can do it like this:

import java.time.*;
import java.time.format.*;
import java.util.*;

public class StackOverflow
{
    public static void main(String[] args)
    {
        String date = "2020-11-09T06:41:01-0800"; 
        ZonedDateTime zdt = ZonedDateTime.parse(date,DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ"));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("h:mm a",Locale.getDefault());
        String formatted = formatter.format(zdt);
        System.out.println(formatted);
    }
}

it prints your desired result:

6:41 AM
Ankit
  • 682
  • 1
  • 6
  • 14
-1
public static void main(String[] args) {
        System.out.println(getTimeByAMorPMFormat("2020-11-09T06:41:01-0800", "yyyy-MM-dd'T'hh:mm:ss-ssss"));
    }
    public static String getTimeByAMorPMFormat(String time, String format) {
        SimpleDateFormat formatTime = new SimpleDateFormat(format, Locale.ENGLISH);
        SimpleDateFormat formatTime1 = new SimpleDateFormat("HH:mm a");
        String str = "";
        try {
            str = formatTime1.format(formatTime.parse(time));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return str;
    }
ghost
  • 1