2


I am new to Java and was trying to convert a String date to a certain java.util.Date format. The un-parsed String date is in yyyy-MM-dd HH:mm:ss pattern and I want to change it todd-MM-yyyy, for that I wrote some first implementation as follows below:

import java.text.SimpleDateFormat;

public class testDate {

    public void parseDate() {
        try {
            String testDate = "2015-06-19 00:00:00.000000";
            SimpleDateFormat format1 = new SimpleDateFormat("dd-MM-yyyy");
            java.util.Date parsedDate = format1.parse(testDate);
            System.err.println(parsedDate);
        } 
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        testDate td = new testDate();
        td.parseDate();
    }

}

The output Tue Dec 05 00:00:00 IST 24 is unexpected for me as a novice :).

So, I would request to suggest or point out where am I going wrong. Thanks

N.B:- There is a similar post (Convert java.util.Date to String) but it talks about parsing one Date format to another, but not String to a different Date Format

2
  • possible duplicate of Convert java.util.Date to String Commented Jul 20, 2015 at 16:52
  • Thanks, @JFPicard, but the answer there is converting and parsing from Date to Date not String to Date Commented Jul 20, 2015 at 16:55

5 Answers 5

3

You're confusing parsing and rendering the date. You need to parse the date using the format it's in, of course, then you can render it using a different format.

        String testDate = "2015-06-19 00:00:00.000000";
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        java.util.Date parsedDate = format1.parse(testDate);
        SimpleDateFormat format2 = new SimpleDateFormat("dd-MM-yyyy");
        System.err.println(format2.format(parsedDate));
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the info, that worked, accepted your amswer. I could not upvote anybody's post due to my reputation. But wen I have that, will definitely comebacl do so. Thanks
3

The SimpleDateFormat should have the right pattern to parse the given string. If you construct format1 like this

SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

the "parse" call will work correctly on the given date. After that, you are able to format it, to your own needs:

public void parseDate() {
    try {
        String testDate = "2015-06-19 00:00:00.000000";
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.util.Date parsedDate = format1.parse(testDate);

        //change the pattern
        format1.applyPattern("dd-MM-yyyy");

        System.err.println(format1.format(parsedDate));
    } 
    catch (Exception e) {
        e.printStackTrace();
    }
}

Comments

1

Your String does not seem to be in the same format as your SimpleDateFormat.

try

 String testDate = "19-06-2015";
 SimpleDateFormat format1 = new SimpleDateFormat("dd-MM-yyyy");
 Date date;
try {
    date = format1.parse(testDate);
      System.out.println(date);
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Comments

1

You're not parsing the input date correctly. format1 is supposed to be the format of the incoming date, not what you want the date to be.

try {
  String testDate = "2015-06-19 00:00:00.000000";
  SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
  java.util.Date parsedDate = format1.parse(testDate);
  SimpleDateFormat format2 = new SimpleDateFormat("dd-MM-yyyy");
  System.err.println(format2.format(parsedDate));
} 

Comments

1

When you parse the string you want a Date object that is a representation of your original date. Therefore, you need to parse it with the format it comes with. You then need another SimpleFormat with your target format for the output.

So, try this:

import java.text.SimpleDateFormat;

public class testDate {

    public void parseDate() {
        try {
            String testDate = "2015-06-19 00:00:00.000000";
            // not sure about the Millisecond part ... might be SSSSSS
            SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
            java.util.Date parsedDate = format1.parse(testDate);

            SimpleDateFormat format2 = new SimpleDateFormat("dd-MM-yyyy");
            String ouput = format2.format(parsedDate)
            System.err.println(parsedDate);
        } 
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        testDate td = new testDate();
        td.parseDate();
    }
}

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.