Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does java intercept a date from an irregular string

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "how to intercept dates from irregular strings by java". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Truncate the date from an irregular string

Recently, I need to call the interface remotely in the project to intercept the date from the String string. After thinking about it for a long time, I finally solved the problem of java8 by using the new feature of java8.

Code

The obtained string, for example:

The effluent of the fourth phase of the coking plant (test outlet). On 2019-10-19, the average value of pH value exceeded the standard, value: 5.00.

Coke oven pushing and dedusting (test outlet) at 14:00:00 on 2019-7-14, the converted average value of sulfur dioxide exceeded the standard, value: 51.27.

...

String for the upgraded version:

According to the test of so-and-so company, at 03:00 on August 27, 2018, the hourly concentration of NOX exceeded the standard for 4 times.

The test data of so-and-so company have been removed from the iron yard (steel plate). From 14:23 on November 18, 2017 to 15:13 on November 21, 2019, it has been offline for 90 hours. "

/ / intercept time processing-java8 / / content is the acquired string List lists = Collections.singletonList (content); String beginDate = lists.stream () .map (e-> {int lastIndexOf = e.lastIndexOf (": 00")) Int indexOf = e.indexOf (")"); String substring = e.substring (indexOf + 1, lastIndexOf + 3); String trim = substring.replace (",", ") .trim (); return trim;}) .findFirst () .orElse (null) / * * upgrade version * intercept time processing-- java8 * content is the obtained string * / List lists = Collections.singletonList (content) String beginDate = lists.stream () .map (e-> {int lastIndexOf; int indexOf; String trim; lastIndexOf = e.lastIndexOf (to)) If (lastIndexOf = =-1) {lastIndexOf = e.lastIndexOf (": 00"); if (lastIndexOf = =-1) {/ / prevent irregular string time error to empty return "" } indexOf = e.indexOf (","); trim = e.substring (indexOf + 1, lastIndexOf + 3);} else {indexOf = e.indexOf (",") String substring = e.substring (indexOf + 1, lastIndexOf); trim = substring.replace (",", ") .trim ();} return trim;}) .findFirst () .orElse (null) String b = beginDate.replaceAll ("[year, month]", "-"); / / to null correlation processing-the ternary operator resultDto is a custom entity resultDto.setBeginDate (b.equals (")? Null: B)

Note that the substring () package is left but not right, and the punctuation in the string is in Chinese, which should be noted.

There is no upgrade output.

Convert irregular dates in yyyy/MM/dd form into regular form

For example:

Converted from 2019-7-2 to 2019-07-02

The code is as follows: public static String toDate (String str) {String pattern = "\\ d {4}\ /\ d {1Magne2}\ /\ d {1jie2}"; if (str! = null & & Pattern.matches (pattern, str)) {String [] date = StringUtils.split (str, "\ /"); Calendar C1 = Calendar.getInstance () C1.set (Integer.parseInt (date [0]), Integer.parseInt (date [1])-1, Integer.parseInt (date [2])); SimpleDateFormat sdf = new SimpleDateFormat ("yyyy/MM/dd"); return sdf.format (c1.getTime ());} return null;}

\\ d {4}\\ /\\ d {1Magne2}\ /\ d {1Magne2}: in java,\\ represents the backslash of a regular expression,\ / indicates the escape of the slash, and\ represents a normal backslash.

That's all for "how to intercept dates from irregular strings by java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report