2

I have date string value like this: Fri, 27-Sep-2013 08:29:59 GMT And i need, to convert it in Date format I tried like this:

private Date modifyDateLayout(String inputDate) {
        DateFormat format = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        format.setTimeZone (TimeZone.getTimeZone ("GMT"));
        Date d = null;
        try {
        d = format.parse(inputDate);
        } catch (ParseException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return d;
    }

But it didn't work, and e=null Where am I wrong? Thanks for the help

2
  • 1) try DateFormat format = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss.SSS'Z'"); without 'T' and set timezone before setting a date template. 2)Maybe incoming String is not valid May be this java67.blogspot.com/2013/01/… help you Commented Sep 27, 2013 at 8:34
  • first solution doesn't work too... The String is valid, I copied string in question from debug Commented Sep 27, 2013 at 8:42

6 Answers 6

2

Try this works perfectly

private Date modifyDateLayout(String inputDate) {
        SimpleDateFormat format = new SimpleDateFormat("EEE, dd-MMM-yyyy hh:mm:ss");
        format.setTimeZone(TimeZone.getDefault());
        Date d = null;
        try {
            d = format.parse(inputDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d;
    }

Call

modifyDateLayout("Fri, 27-Sep-2013 08:29:59 GMT");
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I don't know reason, but steel returns null. Even full copy of you answer, and I can't understood this... Thanks for the help!
1

The format should be EEE, dd-MMM-yyyy hh:mm:ss ZZZ

Comments

1

try this

DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
System.out.println(format.format(now));
String s = format.format(now);
String result = s.substring(0, 26) + ":" + s.substring(27);
System.out.println("Result: "+result);

Comments

1

This should be your format. EEE, dd-MMM-yyyy hh:mm:ss ZZZ

Here is the working output.

2 Comments

I can't understand... I copied all you code (even with string const) and steel don't work!
in catch e = null! :o
1

Try this....

private Date modifyDateLayout(String inputDate) {
    DateFormat format = new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss z");
    format.setTimeZone(TimeZone.getTimeZone("GMT"));
    Date d = null;
    try {
        d = format.parse(inputDate);
    } catch (ParseException e) {
        e.printStackTrace(); // To change body of catch statement use File |
                                // Settings | File Templates.
    }
    return d;
}

Comments

1

This is working. Just set your time according to GMT and it will give you your local time.

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


public class Time {
    private Date modifyDateLayout(String inputDate) {
        DateFormat format = new SimpleDateFormat ("EEE, dd-MMM-yyyy hh:mm:ss zzz");
        format.setTimeZone (TimeZone.getTimeZone ("GMT"));

        Date d = null;
        try {
        d = format.parse(inputDate);
        System.out.println(d);
        } catch (ParseException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return d;
    }
    public static void main(String[] args) {
        //new Time().modifyDateLayout("Fri, 27-Sep-2013 08:29:59 GMT");
        new Time().modifyDateLayout("Fri, 17-Apr-2015 15:45:59 GMT");
    }
}

1 Comment

@Vitality you had made mistake in your second line DateFormat format = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

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.