简体   繁体   English

将日期格式从yyyy-mm-dd更改为yyyy / mm / dd

[英]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. 我能够按预期获得输出,但是当我尝试将其设置回i时,需要所需的输出为DATE类型而不是String。 But if the parse it ot try casting it with DATE,like this 但是,如果解析它,则尝试使用DATE进行转换,就像这样

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. 我需要输出为2013/08/23格式,并且应该为DATE数据类型。 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. format返回格式化日期。 It doesn't modify the Date object. 它不会修改Date对象。 If you want to print out the date in the format, you have to print out the response of format . 如果你想要的格式打印出日期,你要打印出的响应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. 我需要输出为2013/08/23格式,并且应该为DATE数据类型。 Is there any way i can achieve this. 有什么办法可以实现这一目标。

No, that is not possible. 不,那是不可能的。

A Date object only contains a date value. Date对象仅包含日期值。 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 . 要以某种格式显示日期,可以使用SimpleDateFormatSimpleDateFormat对象上指定格式,然后使用它将Date转换为String

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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