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?

4
  • Create a SimpleDateFormat instance, parse your Strings into Date objects, then use another SimpleDateFormat to output in the new format Commented Nov 10, 2020 at 5:32
  • please read this article about DateFormatting: javatpoint.com/java-string-to-date Commented Nov 10, 2020 at 5: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. Commented Nov 10, 2020 at 8: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. Commented Nov 10, 2020 at 8:13

3 Answers 3

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);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I believe “-0800” is a zone offset, not “fraction of second”, if I am right, you might want to replace “-SSSS” with “Z”...
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.
Added the suggestions shown by nkrivenko and Ole V.V to make the answer more accurate..
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

1 Comment

@srini You're welcome!
-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;
    }

3 Comments

you should use SimpleDateFormat and Locale API to solved this question.
Please see Ole V.V.'s comment why you prefer new java.time API to the old one.
@nkrivenko Oh,i see .Thanks for your opionion ,Modern java date and java api are really better than this one

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.