How to use SimpleDateFormat In Java
- In this approach, we are using the SimpleDateFormat class from Java’s standard library to parse a user-inputted date and time string (userInput) in the format “yyyy-MM-dd HH:mm:ss” into a Date object (res).
- The SimpleDateFormat object (dFormat) is initialized with the desired date-time format, and the parse method is used to convert the string into a Date object based on this format.
- Finally, the parsed Date object is printed to the console using println.
In the below example, the SimpleDateFormat library is used to convert String to date time.
// importing necessary classes from Java
import java.text.SimpleDateFormat
import java.util.Date
// Creating object GFG
object GFG {
// Main method
def main(args: Array[String]): Unit = {
// Input string representing date and time
val userInput = "2024-03-27 13:30:00"
// Creating SimpleDateFormat object with the desired date-time format
val dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
// Parsing the input string to obtain a Date object
val res = dFormat.parse(userInput)
// Printing the parsed Date
println("Parsed Date: " + res)
}
}
Output:
How to Convert String to Date Time in Scala?
In this article, we will learn how to convert String to DateTime in Scala. Converting String to DateTime consists of parsing a textual representation of date and time into a structured DateTime object for further manipulation and processing in Scala programs.
Table of Content
- Using SimpleDateFormat
- Using SimpleDateFormat with Calender
- Using Simple Parsing
- Using Regular Expressions