Java DateFormat parse(string , ParsePosition)方法及示例

来源:这里教程网 时间:2026-02-17 20:44:45 作者:

Java DateFormat parse(string , ParsePosition)方法及示例

java.text包中的DateFormat类是一个抽象的类,用于格式化和解析任何地区的日期。它允许我们将日期格式化为文本并将文本解析为日期。DateFormat类提供了许多功能来获取、格式化、解析默认的日期/时间。

注意: DateFormat类扩展了Format类,这意味着它是Format类的一个子类。由于DateFormat类是一个抽象类,因此,它可以用于日期/时间格式化子类,以独立于语言的方式格式化和解析日期或时间。

包-视图

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 : 这是一个字符串类型,指的是要被解析以产生日期的字符串。position : 这是ParsePosition对象的类型,指的是解析的起始索引信息。

返回类型: 返回从字符串中解析出来的日期,如果出现错误则返回空值。

例子 1 :

// Java Program to Illustrate parse() Method// of DateFormat Class // Importing required classesimport java.text.*;import java.util.Calendar; // Main classpublic 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();        }    }}

输出

The unparsed string is: 10/ 27/ 16Time parsed: Thu Oct 27 00:00:00 UTC 2016

例2 :

// Java Program to Illustrate parse() Method// of DateFormat Class // Importing required classesimport java.text.*;import java.util.Calendar; // Main classpublic 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/ 19Time parsed: Tue Jan 29 00:00:00 UTC 2019

相关推荐