score:35

Accepted answer

You may use formatting:

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

score:1

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)

score:4

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

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

score:5

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());    

score:6

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

score:18

Since Java 8, you can also use LocalDate class:

println(java.time.LocalDate.now)

OR

java.time.LocalDate.now.toString

score:79

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

Related Query

More Query from same tag