cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
cancel
Showing results for 
Search instead for 
Did you mean: 

Get current date as string in databricks using scala

Vibhor
Contributor

I want to get current date in scala as a string for example today current date is 3rd jan, want to store it as a new variable dynamically as below, how to get it.

val currdate : String = “20220103”

when I am using val currdate = Calendar.getInstance.getTime then am not getting output in desired format as above.

7 REPLIES 7

Anonymous
Not applicable

@Vibhor Sethi​ - Thanks for your question. We will circle back around if the other members don't reply first. Thanks in advance for your patience.

Hi thanks. Could you please circle back around the question.

Anonymous
Not applicable

@Vibhor Sethi​ - I'll pass this on to the team. Thank you. 🙂

Kaniz
Community Manager
Community Manager

Hi @Vibhor Sethi​ ,

This is how it's done using contemporary java.time library.

import java.time.LocalDate
import java.time.format.DateTimeFormatter 
 
val currdate:String = 
LocalDate.now.format(DateTimeFormatter.ofPattern("yyyyMMdd"))

Older utilities like Calendar and SimpleDate still work (mostly) but should be avoided.

Thanks it worked

Kaniz
Community Manager
Community Manager

Hi @Vibhor Sethi​ , Would you like to mark it as the best answer?

Anonymous
Not applicable

Hey @Vibhor Sethi​ 

Hope you are well!

Thank you for posting your question and letting us know that you were able to resolve the issue. Would you be happy to mark it as the best solution? It would be really helpful for the other members too.

Cheers!

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.