2

I want to convert a Timestamp value which is passed as String to SimpleDateFormat Object into Time Value but it throws a Unparseable date exception.

The Value which i am passing is Thu Jan 1 17:45:00 UTC+0530 1970

Bur i am getting an Exception as mentioned below:

java.text.ParseException: Unparseable date: "Thu Jan 1 17:45:00 UTC+0530 1970"

Please find the below code which i have implemented(Not Working):

static SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
static SimpleDateFormat inputFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
static SimpleDateFormat outputFormatTime = new SimpleDateFormat("HH:mm:ss");

public static String convertUtcDateStringToTime(String utcDateValue) throws Exception
    {
        Date parsedDate = dateFormat.parse(utcDateValue);
        String returnDate=outputFormatTime.format(inputFormat.parse(parsedDate.toString()));
        return returnDate;
    }

If i use the below code it works fine for me(Working) but its a depreciated function of Date which i want to avoid..

@SuppressWarnings("deprecation")
public static String convertUtcDateStringToTime(String utcDateValue) throws Exception
{
    Date dateValue=new Date(utcDateValue);
    Date parsedDate = dateFormat.parse(dateValue.toString());
    String returnDate=outputFormatTime.format(inputFormat.parse(parsedDate.toString()));
    return returnDate;
}

Please Guide Me To implement the logic where i have missed. Thanks in advance.

4 Answers 4

3

with an addition to the answers if the formatting string is like this

"EEE MMM dd HH:mm:ss z yyyy"

then your input string should be

"Thu Jan 1 17:45:00 +0530 1970"

note that the "UTC" is skipped as implicitly it refers to the RFC 822 time zone

Sign up to request clarification or add additional context in comments.

Comments

2

First of all, your 2nd SimpleDateFormat object, is not needed at all. You are doing the extra work, which is not needed. So, remove this variable:

static SimpleDateFormat inputFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);  // Not needed.

Secondly DateFormat#format(Date) methods takes a Date object. You are passing it a String. That wouldn't work. That is why you don't need the above object. There is no need to do a inputFormat.parse(parsedDate.toString()) again.


Now, the format to parse your current string should be:

"EEE MMM dd HH:mm:ss 'UTC'z yyyy"

You need to give the UTC in quotes, before z. Or for more general case:

"EEE MMM dd HH:mm:ss zZ yyyy"

So, your code should be like:

static SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zZ yyyy", Locale.US);
static SimpleDateFormat outputFormatTime = new SimpleDateFormat("HH:mm:ss");

public static String convertUtcDateStringToTime(String utcDateValue) throws Exception
    {
        Date parsedDate = dateFormat.parse(utcDateValue);
        String returnDate=outputFormatTime.format(inputFormat);
        return returnDate;
    }

Comments

1

You input dateformat needs to be

SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zZ yyyy", Locale.US);

The other formatting is all upto, you based on your requirements.

Comments

1

java.time

In March 2014, Java 8 introduced the modern, java.time date-time API which supplanted the error-prone legacy java.util date-time API. Any new code should use the java.time API.

Solution using modern date-time API

Parse your date-time string to an OffsetDateTime using a DateTimeFormatter with an appropriate pattern e.g. DateTimeFormatter.ofPattern("EEE MMM d HH:mm:ss VVXX uuuu", Locale.ENGLISH).

You can get the LocalTime part of it using OffsetDateTime#toLocalTime. Note that the default format of LocalTime#toString removes the second and the fraction-of-second parts if they are zero; so, if you need them in your output string, you will have to use a DateTimeFormatter e.g. DateTimeFormatter.ofPattern("HH:mm:ss", Locale.ENGLISH).

Demo:

class Main {
    private static final DateTimeFormatter parser = DateTimeFormatter.ofPattern("EEE MMM d HH:mm:ss VVXX uuuu",
            Locale.ENGLISH);
    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss", Locale.ENGLISH);

    public static void main(String args[]) {
        OffsetDateTime odt = OffsetDateTime.parse("Thu Jan 1 17:45:00 UTC+0530 1970", parser);
        System.out.println(odt);

        LocalTime time = odt.toLocalTime();
        System.out.println(time);

        // Formatting as desired
        String formatted = time.format(formatter); // or odt.format(formatter)
        System.out.println(formatted);
    }
}

Output:

1970-01-01T17:45+05:30
17:45
17:45:00

Online Demo

Note: If for some reason, you need an instance of java.util.Date, let java.time API do the heavy lifting of parsing your date-time string and convert odt from the above code into a java.util.Date instance using Date date = Date.from(odt.toInstant()).

Learn more about the modern Date-Time API from Trail: Date Time.

A useful link: Always specify a Locale with a date-time formatter for custom formats

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.