java 如何使用类日期获取格式为“YYYY-MM-DD 00:00:00.0”的数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17649941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 14:32:07  来源:igfitidea点击:

How can i get data in format "YYYY-MM-DD 00:00:00.0" using class Date?

javadate

提问by Jane Makedonskaya

How can i get data in format "YYYY-MM-DD 00:00:00.0" using class Date (it's extremly important to use exactly this class)?

如何使用类 Date 以“YYYY-MM-DD 00:00:00.0”格式获取数据(完全使用此类非常重要)?

I tried to do everything i can think of:

我试图做我能想到的一切:

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
df.format(date)

and

String pattern = "yyyy-MM-dd HH:mm:ss.S";
SimpleDateFormat format = new SimpleDateFormat(pattern);
try {
Date date = format.parse("2011-01-18 00:00:00.0");
} catch (ParseException e) {
e.printStackTrace();
}

byt when i print date using logger i get this format "Tue Sep 30 00:00:00 MSK 1913".

当我使用记录器打印日期时,我得到了这种格式“Tue Sep 30 00:00:00 MSK 1913”。

回答by Ruchira Gayan Ranaweera

Try this

试试这个

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
    Date date = format.parse("2011-01-18 00:00:00.0");
    System.out.println(format.format(date));

回答by Niels Bech Nielsen

Are you sure you want the hours, minutes, secs to be zeroes?

您确定要将小时、分钟、秒设为零吗?

Or do you mean the pattern yyyy-MM-dd HH:mm:ss ?

或者你的意思是模式 yyyy-MM-dd HH:mm:ss ?

The date class is always independent of the formatting. It only needs to be translated when you print it, like this:

日期类始终与格式无关。它只需要在打印时进行翻译,如下所示:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String out = df.format(date)
System.out.println(out);

Or do you want to strip the time out of the date object? or something.

或者你想从日期对象中去除时间?或者其他的东西。

回答by Kent

You are confused by Date.toString()and SimpleDateFormat.format()

你被Date.toString()和迷惑了SimpleDateFormat.format()

An object of Date (java.util.Date)has noformat information. If you call date.toString(), (which is called by your logger), you got default representation of this object, you have seen what it is.

的目的Date (java.util.Date)没有格式的信息。如果您调用date.toString(), (由您的记录器调用),您将获得此对象的默认表示,您已经看到它是什么。

However, SimpleDateFormat.format()will give you a string as return value, this value will format the Date object with a pattern defined by SimpleDateFormat.

但是,SimpleDateFormat.format()会给您一个字符串作为返回值,该值将使用由SimpleDateFormat.

In your code, you first parsed the string, with certain pattern, to get the date object. If it was successful, you got the Date object, here, for this date object, you don't have any format information, even if you have defined a pattern to parse the input string. If you want to print/output (to string again) the date object, you have to use the SimpleDateFormat.format()method.

在您的代码中,您首先使用特定模式解析字符串以获取日期对象。如果成功,你就得到了 Date 对象,在这里,对于这个日期对象,你没有任何格式信息,即使你已经定义了一个模式来解析输入字符串。如果要打印/输出(再次字符串)日期对象,则必须使用该SimpleDateFormat.format()方法。

回答by Nallamachu

I hope the below one will solve your problem when you need to do for dynamic dates.

当您需要处理动态日期时,我希望下面的一个可以解决您的问题。

Date today = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
String formattedDate = sdf.format(today);
System.out.println(formattedDate);
//Above line output (formatted String object) is 2017-12-29 00:00:00
System.out.println(format.format(formattedDate));
//Above line output(parsed Date object) is Fri Dec 29 00:00:00 IST 2017

For Date object you can't get the output as yyyy-MM-dd 00:00:00, but you will get this format in String object type.

对于 Date 对象,您无法获得yyyy-MM-dd 00:00:00的输出,但您将以 String 对象类型获得此格式。