Reputation: 15490
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
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
Reputation: 1071
Since Java 8, you can also use LocalDate
class:
println(java.time.LocalDate.now)
OR
java.time.LocalDate.now.toString
Upvotes: 21
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
Reputation: 2180
val dateFormatter = new SimpleDateFormat("dd/MM/yyyy hh:mm aa")
var submittedDateConvert = new Date()
submittedAt = dateFormatter.format(submittedDateConvert)
Upvotes: 5
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
Reputation: 15742
scala> java.time.LocalDate.now
res4: java.time.LocalDate = 2014-07-11
Upvotes: 81
Reputation: 632
You may use formatting:
val format = new SimpleDateFormat("d-M-y")
println(format.format(Calendar.getInstance().getTime()))
Upvotes: 36