Java中的 DateFormat parse( 字符串 , ParsePosition) 方法及示例
Java.text 包的 DateFormat 类是一个抽象类,用于格式化和解析任何语言环境的日期。它允许我们将日期格式化为文本并将文本解析为日期。 DateFormat 类提供了许多功能来获取、格式化、解析默认日期/时间。
Note: DateFormat class extends Format class that means it is a subclass of Format class. Since DateFormat class is an abstract class, therefore, it can be used for date/time formatting subclasses, which format and parses dates or times in a language-independent manner.
包视图:
java.text Package
DateFormat Class
parse(string , ParsePosition) Method
DateFormat 类的parse(String the_text , ParsePosition position )方法用于从字符串中解析文本以生成日期。该方法从起始位置给定的索引开始解析文本。
句法:
public abstract Date parse(String the_text, ParsePosition position)
参数:它需要2个参数:
- the_text :这是String类型,指的是要解析以生成日期的字符串。
- position :这是ParsePosition对象类型,是指解析的起始索引的信息。
返回类型:返回从字符串解析的日期,如果出错则返回 Null。
示例 1:
Java
// Java Program to Illustrate parse() Method
// of DateFormat Class
// Importing required classes
import java.text.*;
import java.util.Calendar;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating object of DateFormat class inside main()
DateFormat DFormat
= new SimpleDateFormat("MM/ dd/ yy");
// Try block to check for exceptions
try {
Calendar cal = Calendar.getInstance();
// Parsing date From string
// using parse() method of DateFormat class
// Custom string date
String dt = "10/ 27/ 16";
// Printing the above unparsed date
System.out.println("The unparsed"
+ " string is: " + dt);
// Parsing date using parse() method
cal.setTime(DFormat.parse(dt));
// Printing the parsed time
System.out.println("Time parsed: "
+ cal.getTime());
}
// Catch block to handle exceptions
catch (ParseException except) {
// Display exceptions with line number
// using printStackTrace() method
except.printStackTrace();
}
}
}
Java
// Java Program to Illustrate parse() Method
// of DateFormat Class
// Importing required classes
import java.text.*;
import java.util.Calendar;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an object of DateFormat class
DateFormat DFormat
= new SimpleDateFormat("MM/ dd/ yy");
// Try bloc kto check for exceptions
try {
// Getting instance from calendar
Calendar cal = Calendar.getInstance();
// Parsing date from string
// using parse() method
String dt = "01/ 29/ 19";
// Displaying the unparsed date
System.out.println("The unparsed"
+ " string is: " + dt);
// Parsing date
cal.setTime(DFormat.parse(dt));
System.out.println("Time parsed: "
+ cal.getTime());
}
// Catch block to handle the exceptions
catch (ParseException except) {
// Display exception with line number
// using printStackTrace() method
except.printStackTrace();
}
}
}
输出:
The unparsed string is: 10/ 27/ 16
Time parsed: Thu Oct 27 00:00:00 UTC 2016
示例 2:
Java
// Java Program to Illustrate parse() Method
// of DateFormat Class
// Importing required classes
import java.text.*;
import java.util.Calendar;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an object of DateFormat class
DateFormat DFormat
= new SimpleDateFormat("MM/ dd/ yy");
// Try bloc kto check for exceptions
try {
// Getting instance from calendar
Calendar cal = Calendar.getInstance();
// Parsing date from string
// using parse() method
String dt = "01/ 29/ 19";
// Displaying the unparsed date
System.out.println("The unparsed"
+ " string is: " + dt);
// Parsing date
cal.setTime(DFormat.parse(dt));
System.out.println("Time parsed: "
+ cal.getTime());
}
// Catch block to handle the exceptions
catch (ParseException except) {
// Display exception with line number
// using printStackTrace() method
except.printStackTrace();
}
}
}
输出
The unparsed string is: 01/ 29/ 19
Time parsed: Tue Jan 29 00:00:00 UTC 2019