Govind Singh
Govind Singh

Reputation: 15490

How to get the current date without time in scala

I want to get the current date so I used:

Calendar.getInstance().getTime()

But as it .getTime() it is returning:

Fri Jul 11 15:07:03 IST 2014

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

Upvotes: 40

Views: 121144

Answers (7)

azatprog
azatprog

Reputation: 314

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

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)

Upvotes: 1

karthik r
karthik r

Reputation: 1071

Since Java 8, you can also use LocalDate class:

println(java.time.LocalDate.now)

OR

java.time.LocalDate.now.toString

Upvotes: 21

vijayraj34
vijayraj34

Reputation: 2415

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

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

Upvotes: 6

Nilesh
Nilesh

Reputation: 2180

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

Upvotes: 5

Karthik
Karthik

Reputation: 167

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

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

Upvotes: 5

Knut Arne Vedaa
Knut Arne Vedaa

Reputation: 15742

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

Upvotes: 81

artie
artie

Reputation: 632

You may use formatting:

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

Upvotes: 36

Related Questions