40

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.

7 Answers 7

81
scala> java.time.LocalDate.now res4: java.time.LocalDate = 2014-07-11 
Sign up to request clarification or add additional context in comments.

3 Comments

error: object time is not a member of package java
Use Java 8 or higher.
^ Java 8+ might not be an option for everybody. This answer should have been qualified with that requirement.
36

You may use formatting:

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

Comments

21

Since Java 8, you can also use LocalDate class:

println(java.time.LocalDate.now) 

OR

java.time.LocalDate.now.toString 

2 Comments

Thanks!! scala> java.time.LocalDate.now.toString /n res2: String = 2018-11-14
@TonyFraser pls add it as another answer. I would like to upvote it
6

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

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

Comments

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

Comments

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

2 Comments

Take a moment to read through the editing help in the help center. Formatting on Stack Overflow is different than other sites.
While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations!
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) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.