最一般意義上的解析是從一些數據中提取必要的信息,最常見的是文本數據。Java 中的解析是什麼?許多 Java 類都具有parse()方法。通常parse()方法接收一些字符串作為輸入,從中“提取”必要的信息並將其轉換為調用類的對象。例如,它收到一個字符串並返回“隱藏”在該字符串中的日期。在這篇文章中,我們將看看parse()的 10 個有用的變體。
0.parseInt()
讓我們從最流行的parse()方法之一開始,它不完全是parse(),而是parseInt()。Java parseInt()方法用於從特定字符串中獲取原始數據類型。換句話說,它將字符串轉換為數字。parseInt ()可以有一個或兩個參數。這是parseInt()的語法:
static int parseInt(String s)
static int parseInt(String s, int radix)
其中s是表示帶符號的十進制值和基數的數字系統基數的字符串。請記住,沒有默認基值——您需要輸入 2 到 36 範圍內的一個值。這是一個示例。如何使用 ParseInt() 進行解析:
public class ParseInt0 {
public static void main(String args[]){
int x = Integer.parseInt("12");
double c = Double.parseDouble("12");
int b = Integer.parseInt("100",2);
System.out.println(Integer.parseInt("12"));
System.out.println(Double.parseDouble("12"));
System.out.println(Integer.parseInt("100",2));
System.out.println(Integer.parseInt("101", 8));
}
}
輸出是:
12 12.0 4 65
1.句點parse()方法
Period是一個 Java 類,用於根據年、月和日對時間量進行建模,例如“3 年、5 個月和 2 天”。它有一個parse()方法來從文本中獲取句點。這是period parse()的語法
public static Period parse(CharSequence text)
CharSequence 是一個接口,由字符串實現。因此,您可以在parse()方法中將字符串用作文本元素。確定字符串應該採用正確的格式以返回 Period 類的對象。這種格式是PnYnMnD。其中 Y 代表“年”,M 代表“月”,D 代表“日”。N 是對應於每個週期值的數字。
- 該方法有一個參數——一個文本值。
- Parse()返回一個 Period 值,其中字符串的值成為參數。
- 作為例外,如果字符串值不符合句點的結構,句點 parse()可以返回 DateTimeParseException。
import java.time.Period;
public class ParseDemo1 {
public static void main(String[] args)
{
//Here is the age String in format to parse
String age = "P17Y9M5D";
// Converting strings into period value
// using parse() method
Period p = Period.parse(age);
System.out.println("the age is: ");
System.out.println(p.getYears() + " Years\n"
+ p.getMonths() + " Months\n"
+ p.getDays() + " Days\n");
}
}
}
}
輸出是:
年齡是:17歲9個月5天
2.SimpleDateFormatParse()方法
SimpleDateFormat是一個用於以區域設置敏感方式格式化和解析日期的類。SimpleDateFormat parse()方法將字符串分解為日期標記並返回相應格式的數據值。該方法從開發人員定義的索引處開始解析字符串。這是SimpleDateFormat parse()的語法:
public Date parse(String the_text, ParsePosition position)
該方法有兩個參數:
- Position:起始索引處的數據,始終為 ParsePosition 對像類型。
- the_text:定義了該方法要解析的字符串,是一個String類型的值。
// Parsing strings into the Date format with two different patterns import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ParseDemo2 {
public static void main(String[] args) throws ParseException {
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("dd/MM/yyyy");
//simpleDateFormat1.setLenient(false);
Date date1 = simpleDateFormat1.parse("010/14/2020");
System.out.println(date1);
Date date2 = simpleDateFormat2.parse("14/10/2020");
System.out.println(date2);
ParsePosition p1 = new ParsePosition(18);
ParsePosition p2 = new ParsePosition(19);
ParsePosition p3 = new ParsePosition(5);
String myString = "here is the date: 14/010/2020";
Date date3 = simpleDateFormat2.parse(myString,p1);
Date date4 = simpleDateFormat2.parse(myString,p2);
Date date5 = simpleDateFormat2.parse(myString,p3);
System.out.println(date3);
System.out.println(date4);
System.out.println(date5);
}
}
輸出是:
EEST 2020 年 10 月 14 日星期三 00:00:00 EEST 2020 年 10 月 14 日星期三 00:00:00 EEST 2020 年 10 月 14 日星期三 00:00:00 2020 年 EEST 10 月 4 日星期日 00:00:00 null
最後一個為空,因為沒有從第 5 個位置開始的日期模式。順便說一下,如果您嘗試解析沒有位置的 date5,例如Date date5 = simpleDateFormat2.parse(myString),您將得到一個異常:
線程“main”中的異常 java.text.ParseException:無法解析的日期:“這裡是日期:14/010/2020”,位於 ParseDemo2.main 的 java.base/java.text.DateFormat.parse(DateFormat.java:396) (ParseDemo2.java:22)
3. LocalDate 的 parse() 方法
LocalDate是Java 8中出現的一個類,用來表示年-月-日等日期(也可以訪問day-of-year、day-of-week和week-of-year)。LocalDate 不代表時間或時區。 LocalDate parse()方法有兩個變體。它們都有助於將字符串轉換為新的 Java 8 日期 API — java.time.LocalDate。解析(CharSequence 文本、DateTimeFormatter、格式化程序)
此方法使用特定的格式化程序解析字符串以獲得 LocalDate 的實例。下面是該方法的語法:
public static LocalTime parse(CharSequence text,
DateTimeFormatter formatter)
該方法有兩個參數——將要解析的文本和開發人員將應用的格式化程序。作為返回值,該方法返回一個將被識別為本地白天時間的 LocalTime 對象。如果文本值無法通過解析,系統將拋出 DayTimeParseException。讓我們看一個使用帶有兩個參數的LocalDate parse()的代碼示例:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class ParserDemo3 {
public static void main(String[]args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
String date = "14/10/2020";
LocalDate localDate = LocalDate.parse(date, formatter);
System.out.println("parsed local date: " + localDate);
System.out.println("formatted local date: " + formatter.format(localDate));
}
}
輸出是:
解析的本地日期:2020-10-14 格式化的本地日期:14/10/2020
帶有一個參數的LocalDate parse()方法具有以下語法:
public static LocalTime parse(CharSequence text)
此方法不需要指定格式化程序。開發人員在括號中輸入字符串值後,系統將自動使用 DateTimeFormatter.ISO_LOCAL_DATE。該方法只有一個參數——一個 CharSequence 文本。您可以在此處使用字符串值。確保它不為 null 並尊重格式化程序的結構。如果無法解析字符串,開發人員會收到 DateTimeExceptionAlert。這是LocalDate parse()應用程序的示例:
import java.time.*;
public class ParseDemo3 {
public static void main(String[] args)
{
// let’s make a new LocalDate object
LocalDate localDate = LocalDate.parse("2020-10-14");
System.out.println("LocalDate : " + localDate);
}
}
輸出是:
本地日期:2020-10-14
4. LocalDateTime parse() 方法
LocalDateTime一個日期時間對象,表示日期時間通常被視為年月日時分秒。開發人員還可以使用其他日期和時間字段(一年中的某一天、星期幾和一年中的一周)。這個類是不可變的。時間以納秒精度表示。例如,您可以將值“2020 年 11 月 17 日 13:30.30.123456789”存儲在 LocalDateTime 中。此類與表示時區無關。它是一個標準的日期表示加上本地時間。 LocalDateTime parse()方法以兩種變體表示:- static LocalDateTime parse(CharSequence text)從文本字符串(例如 2007-12-03T10:15:30)返回 LocalDateTime 的實例。
- static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter)使用特定格式化程序從文本字符串返回 LocalDateTime 的實例。
import java.time.*;
public class ParseDemo11 {
public static void main(String[] args) {
LocalDateTime localDateTime = LocalDateTime.parse("2020-11-17T19:34:50.63");
System.out.println("LocalDateTime is: " + localDateTime);
}
}
輸出是:
LocalDateTime 是:2020-11-17T19:34:50.630
5. ZonedDateTime parse() 方法
ZonedDateTime類表示帶有時區的日期時間。這個類是不可變的。它存儲日期和時間字段,精度為納秒,時區,時區偏移量用於處理不明確的本地日期時間。因此,如果您需要保留諸如“歐洲/巴黎時區 2020 年 10 月 14 日 17:50.30.123456789 +02:00”之類的值,您可以使用 ZonedDateTime。該類通常用於操作基於本地時間的數據。 ZondeDateTime parse()是一個解析器,它將字符串分解為 ISO-8061 系統中的標記。這是解析後將獲得的值的示例:
2020-04-05T13:30:25+01:00 歐洲/羅馬
每當需要高精度數據時都會使用它(畢竟,您獲得的數據精確到納秒級)。該類通常用於操作基於本地時間的數據。讓我們看一下開發人員用來將字符串值轉換為 ZonedDateTime 類 的ZonedDateTime parse()方法的一般語法。
public static ZonedDateTime parse(CharSequence text)
該方法使用的唯一參數是字符串文本。作為返回值,您將獲得一個或一系列 ZonedDateTime 格式的對象。如果在解析過程中出現錯誤或無法解析,則該方法首先返回 DateTimeParseException。還有一個帶有兩個變量的parse()方法。
public static ZonedDateTime parse(CharSequence text, DateFormatter formatter)
此方法使用特定格式化程序從文本值獲取 ZonedDateTime 的實例。默認使用帶一個參數的格式化程序DateTimeFormatter.ISO_LOCAL_TIME的方法。讓我們看一下 ZonedDateTime parse() 的用例:
// An example program that uses
// ZonedDateTime.parse() method
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class ParseDemo4 {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2020-10-15T10:15:30+01:00");
System.out.println(zonedDateTime);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;
String date = "2020-10-15T13:30:25+01:00";
ZonedDateTime zoneDateTime1 = ZonedDateTime.parse(date, dateTimeFormatter);
System.out.println(zoneDateTime1);
}
}
輸出是:
2020-10-15T10:15:30+01:00 2020-10-15T13:30:25+01:00
6. LocalTime parse() 方法
LocalTime類表示一個時間,通常被視為小時-分鐘-秒。此類也是不可變的,例如 ZonedDateTime.Time 以納秒精度表示。例如,值“13:45.30.123456789”可以存儲在 LocalTime 中。有兩種LocalTime parse()方法,具有一個和兩個參數。讓我們來看看兩者:
public static LocalTime parse(CharSequence text)
您可以使用只有一個參數的LocalTime parse(),即您要解析的字符串。在這種情況下,默認使用格式化程序 DateTimeFormatter.ISO_LOCAL_TIME。
Method with two parameters has the next syntax:
public static LocalTime parse(CharSequence text,
DateTimeFormatter formatter)
它使用特定的格式化程序從文本值中獲取 LocalTime 的實例。這兩種方法都以 hh/mm/ss 格式返回 LocalTime 值。注意 DateTimeParceException 警報。它們意味著字符串文本的格式與 LocalTime 對象的格式不對應。這是在生產中使用LocalTime parse()的示例:
import java.time.*;
import java.time.format.*;
public class ParseDemo5 {
public static void main(String[] args)
{
LocalTime localTime
= LocalTime.parse("10:25:30");
// return the output value
System.out.println("LocalTime : "
+ localTime);
// create a formater
DateTimeFormatter formatter
= DateTimeFormatter.ISO_LOCAL_TIME;
LocalTime localTime1
= LocalTime.parse("12:30:50");
// parse a string to get a LocalTime object in return
LocalTime.parse("12:30:50",
formatter);
// print the output
System.out.println("LocalTime : "
+ localTime1);
}
}
7. MessageFormat Parse() 方法
MessageFormat擴展了 Format 類。Format 是一個抽象基類,用於格式化區域設置敏感數據(日期、消息和數字)。MessageFormat 獲取一些對象並格式化它們。然後它將格式化的字符串插入到模式中適當的位置。如果給定索引開頭,則MessageFormat parse()用於獲取字符串值。下面是該方法的一般語法:
public Object[] parse(String source, ParsePosition position)
其中source是要解析的字符串,position是解析的起始索引。這是MessageFormat parse()方法工作的示例:
import java.text.MessageFormat;
import java.text.ParsePosition;
public class ParseDemo7 {
public static void main(String[] args) {
try {
MessageFormat messageFormat = new MessageFormat("{1, number, #}, {0, number, #.#}, {2, number, #.##}");
ParsePosition pos = new ParsePosition(3);
Object[] hash = messageFormat.parse("12.101, 21.382, 35.121", pos);
System.out.println("value after parsing: ");
for (int i = 0; i < hash.length; i++)
System.out.println(hash[i]);
}
catch (NullPointerException e) {
System.out.println("\nNull");
System.out.println("Exception thrown : " + e);
} }
}
8.關卡parse()方法
當程序員使用 Logger 記錄一條消息時,它會以特定的日誌級別進行記錄。有七個內置日誌級別:- 嚴重
- 警告
- 信息
- 配置
- 美好的
- 更精細
- 最好的
public static Level parse(String name)
方法的參數是開發人員要解析的字符串的名稱。它可以是關卡的名稱、它的初始化名稱或其他一些整數。作為回報,程序員會得到一個級別名稱值,對應於初始字符串的名稱值。如果參數包含無法解析的符號,系統將拋出 IllegalArgumentException。如果字符串不包含任何值,開發人員將收到 NullPointerException。這是一段代碼,顯示了Level parse()的實現。
import java.util.logging.Level;
public class ParseDemo6 {
public static void main(String[] args)
{
Level level = Level.parse("500");
System.out.println("Level = " + level.toString());
Level level1 = Level.parse("FINE");
System.out.println("Level = " + level1.toString());
Level level2 = level.parse ("OFF");
System.out.println(level2.toString());
}
}
輸出是:
級別 = 精細級別 = 精細關閉
9.即時parse()方法
Instant類模擬時間線上的單個瞬時點。您可以使用它在您的應用程序中記錄事件時間戳。 Instant parse()從文本值中獲取 Instant 值。該字符串稍後將存儲為 UTC 時區值。系統使用 DateTimeFormatter.ISO_INSTANT 如 2020-10-14T11:28:15.00Z。這是Instant parse()方法的語法:
public static Instant parse(CharSequence text)
要解析字符串並獲得即時信息,開發人員需要確保該字符串包含一些文本。如果它為 null,您將得到一個 DateTimeException。下面是在 Java 中 使用Instant parse的示例:
import java.time.Instant;
public class ParseDemo8 {
public static void main(String[] args) {
Instant instant = Instant.parse("2020-10-14T10:37:30.00Z");
System.out.println(instant);
}
}
輸出是:
2020-10-14T10:37:30Z
10. NumberFormat parse() 方法
java.text.NumberFormat類用於格式化數字。NumberFormat parse()是此類的默認方法。NumberFormat 類的parse ()方法將字符串轉換為數字。開發人員使用它將字符串分解為其組件編號。解析從字符串的開頭開始。如果在調用parse()方法之前調用setParseIntegerOnly(true),如下例所示,那麼只會轉換數字的整數部分。下面是NumberFormat parse()的語法:
public Number parse(String str)
作為參數,該函數接受字符串。解析的返回值為數值。如果無法解析字符串開頭,您將收到 ParseException 警告。要查看NumberFormat parse()方法的應用,請看下面的示例:
import java.text.NumberFormat;
import java.text.ParseException;
public class ParseDemo9 {
public static void main(String[] args) throws ParseException {
NumberFormat numberFormat = NumberFormat.getInstance();
System.out.println(numberFormat.parse("3,141592"));
numberFormat.setParseIntegerOnly(true);
System.out.println(numberFormat.parse("3,141592"));
}
}
輸出是:
3.141592 3
GO TO FULL VERSION