简体   繁体   中英

How to convert java.util.Date with a given format to String instance?


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 to dd-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

NB:- 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

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));

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();
    }
}

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();
}

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));
} 

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM