scala 如何在scala中获取没有时间的当前日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24694849/
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-10-22 06:24:16  来源:igfitidea点击:

How to get the current date without time in scala

scaladatedatetimecalendar

提问by Govind Singh

I want to get the current date so I used:

我想获取当前日期,所以我使用了:

Calendar.getInstance().getTime()

But as it .getTime()it is returning:

但随着它.getTime()返回:

Fri Jul 11 15:07:03 IST 2014

I want only date in any format but without the time.

我只想要任何格式的日期,但没有时间。

采纳答案by artie

You may use formatting:

您可以使用格式:

val format = new SimpleDateFormat("d-M-y")
println(format.format(Calendar.getInstance().getTime()))

回答by Knut Arne Vedaa

scala> java.time.LocalDate.now
res4: java.time.LocalDate = 2014-07-11

回答by karthik r

Since Java 8, you can also use LocalDateclass:

从 Java 8 开始,您还可以使用LocalDate类:

println(java.time.LocalDate.now)

OR

或者

java.time.LocalDate.now.toString

回答by Nilesh

val dateFormatter = new SimpleDateFormat("dd/MM/yyyy hh:mm aa")
var submittedDateConvert = new Date()
submittedAt = dateFormatter.format(submittedDateConvert)

回答by Karthik

You need to use SimpleDate formate method. You can specify which formate you want.

您需要使用 SimpleDate 格式化方法。您可以指定所需的格式。

SimpleDateFormat formatter = new SimpleDateFormat("dd/mm/yy");    
String dateSelected = formatter.format(new Date());    

回答by vijayraj34

In case if you want to format the date in your way.

如果您想以自己的方式格式化日期。

println(DateTimeFormatter.ofPattern("dd-MM-YYYY").format(java.time.LocalDate.now))

回答by azatprog

if someone needs the datein a format so that to be able to add to PostgreSQL here we go:

如果有人需要某种格式的日期以便能够添加到 PostgreSQL 中,我们可以:

import java.util.Calendar
val calendar = Calendar.getInstance
val now = new Timestamp(calendar.getTime.getTime)

or in one line:

或在一行中:

val now = new Timestamp(java.util.Calendar.getInstance.getTime.getTime)