将字符串转换为日期的Java程序
给定一个日期格式的字符串,任务是将此字符串转换为实际日期。这里的主要概念是有助于转换的 parse() 方法。
插图:
Input : string = "2018-10-28T15:23:01Z"
Output: 2018-10-28T15:23:01Z
Input : string = "28 October, 2018"
Output: 2018-10-28
将字符串转换为日期的不同方法
- 使用即时课程
- 使用 DateTimeFormater 类
- 使用 SimpleDateFormat 类
Tip: Must Read the articles DateFormat class and SimpleDateFormat Class.
现在让我们一一讨论上述方法,如下所示:
方法一:使用即时类
Java.time 包中的即时类提供纳秒精度。它类似于 Date 类,但准确性更高。
方法:
- 获取要转换的字符串。
- 创建一个空的即时时间戳对象。
- 使用 Instant.parse() 方法将字符串转换为日期。
- 如果转换成功,则打印日期。
- 如果未成功转换,则抛出 DateTimeParseException。
例子
Java
// Java Program to Convert String to Date
// Using Instant Class
// Importing required classes
import java.time.Instant;
import java.time.format.DateTimeParseException;
// Main class
class GFG {
// Method
// To convert String to Date
public static Instant getDateFromString(String string)
{
// Creating an instant object
Instant timestamp = null;
// Parsing the string to Date
timestamp = Instant.parse(string);
// Returning the converted timestamp
return timestamp;
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Getting the string
String string = "2018-10-28T15:23:01Z";
// Try block to check for exceptions
try {
// Getting the Date from String by
// creating object of Instant class
Instant timestamp = getDateFromString(string);
// Printing the converted date
System.out.println(timestamp);
}
// Catch block to handle exceptions
catch (DateTimeParseException e) {
// Throws DateTimeParseException
// if the string cannot be parsed
System.out.println("Exception: " + e);
}
}
}
Java
// Java program to convert String to Date
// Using DateTimeFormatter Class
// Importing required classes
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
// Main class
class GFG {
// Method 1
// To convert String to Date
public static LocalDate
getDateFromString(String string,
DateTimeFormatter format)
{
// Converting the string to date
// in the specified format
LocalDate date = LocalDate.parse(string, format);
// Returning the converted date
return date;
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Getting the custom string input
String string = "28 October, 2018";
// Getting the format by creating an object of
// DateTImeFormatter class
DateTimeFormatter format
= DateTimeFormatter.ofPattern("d MMMM, yyyy");
// Try block to check for exceptions
try {
// Getting the Date from String
LocalDate date
= getDateFromString(string, format);
// Printing the converted date
System.out.println(date);
}
// Block 1
// Catch block to handle exceptions occuring
// if the String pattern is invalid
catch (IllegalArgumentException e) {
// Display the exception
System.out.println("Exception: " + e);
}
// Block 2
// If the String was unable to be parsed
catch (DateTimeParseException e) {
// Display the exception
System.out.println("Exception: " + e);
}
}
}
Java
// Java Program to Convert String to Date
// Using SimpleDateFormat class
// Importing required classes
import java.text.SimpleDateFormat;
import java.util.Date;
// Main class
class GFG {
// Main driver method
public static void main(String[] args) throws Exception
{
// Custom string as input
String strDate = "29/12/96";
// Creating an object of Date class with reference
// to SimpleDateFormat class and
// lately parsing the above string into it
Date date = new SimpleDateFormat("dd/mm/yyyy")
.parse(strDate);
// Print and display the date corresponding
// to above input string
System.out.print(strDate + " " + date);
}
}
输出:
2018-10-28T15:23:01Z
方法二:使用 DateTimeFormatter 类
方法:
- 获取要转换的字符串和所需的格式。
- 创建一个空的 LocalDate 对象。
- 使用 LocalDate.parse() 方法将字符串转换为日期。
- 如果转换成功,则打印日期
- 如果字符串模式无效,则抛出 IllegalArgumentException。
- 如果未成功转换,则抛出 DateTimeParseException。
例子:
Java
// Java program to convert String to Date
// Using DateTimeFormatter Class
// Importing required classes
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
// Main class
class GFG {
// Method 1
// To convert String to Date
public static LocalDate
getDateFromString(String string,
DateTimeFormatter format)
{
// Converting the string to date
// in the specified format
LocalDate date = LocalDate.parse(string, format);
// Returning the converted date
return date;
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Getting the custom string input
String string = "28 October, 2018";
// Getting the format by creating an object of
// DateTImeFormatter class
DateTimeFormatter format
= DateTimeFormatter.ofPattern("d MMMM, yyyy");
// Try block to check for exceptions
try {
// Getting the Date from String
LocalDate date
= getDateFromString(string, format);
// Printing the converted date
System.out.println(date);
}
// Block 1
// Catch block to handle exceptions occuring
// if the String pattern is invalid
catch (IllegalArgumentException e) {
// Display the exception
System.out.println("Exception: " + e);
}
// Block 2
// If the String was unable to be parsed
catch (DateTimeParseException e) {
// Display the exception
System.out.println("Exception: " + e);
}
}
}
输出:
2018-10-28
方法 3:使用 SimpleDateFormat 类
方法:
- 获取字符串输入并将其存储到字符串中
- 参照 SimpleDateFormat 类创建 Date 类的对象
- 将日期格式解析到其中。
- 打印相应的日期。
例子:
Java
// Java Program to Convert String to Date
// Using SimpleDateFormat class
// Importing required classes
import java.text.SimpleDateFormat;
import java.util.Date;
// Main class
class GFG {
// Main driver method
public static void main(String[] args) throws Exception
{
// Custom string as input
String strDate = "29/12/96";
// Creating an object of Date class with reference
// to SimpleDateFormat class and
// lately parsing the above string into it
Date date = new SimpleDateFormat("dd/mm/yyyy")
.parse(strDate);
// Print and display the date corresponding
// to above input string
System.out.print(strDate + " " + date);
}
}
输出
29/12/96 Fri Jan 29 00:12:00 UTC 96
输出:也当。在终端上运行,如下: