简体   繁体   中英

Change the date format from yyyy-mm-dd to yyyy/mm/dd

 SimpleDateFormat p = new SimpleDateFormat("MM-dd-yyyy");
 String formatDate = p.format(temp.EXPIRATION_DATE()); 

//EXPIRATION_DATE is a DATE data type.

I am able to get the output as expected but when i am trying to set it back i, need the desired output to be in DATE type not the String. But if the parse it ot try casting it with DATE,like this

DateFormat formatter = new SimpleDateFormat("MM-dd-yyyy");
convertedDate = (Date) formatter.format(date);

My output is coming like this:

Fri Aug 23 16:07:11 EDT 2013.

I need the output to be in 2013/08/23 format and it should be in DATE data type. Is there any way i can achieve this. Thanks

package com.sandbox;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Sandbox {

    public static void main(String[] args) throws IOException {
        SimpleDateFormat p = new SimpleDateFormat("yyyy/MM/dd");
        String formatDate = p.format(new Date());
        System.out.println(formatDate);
    }


}

format returns the formatted date. It doesn't modify the Date object. If you want to print out the date in the format, you have to print out the response of format .

使用new SimpleDateFormat("yyyy/MM/dd");

I need the output to be in 2013/08/23 format and it should be in DATE data type. Is there any way i can achieve this.

No, that is not possible.

A Date object only contains a date value. It does not have any information about the format in which to display the date. To display a date in a certain format, you can use a SimpleDateFormat - you specify the format on the SimpleDateFormat object, and then use it to convert the Date to a String .

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