split cut. number of strings

excel in the date shown below, using the read io, debugging is found "12.10.2019" need to be converted to "2019-10-12"

 

In a split method. No cutting, the need to use escape character "\\.", As follows

. 1  Package com.alphajuns.test;
 2  
. 3  Import org.junit.Test;
 . 4  
. 5  / ** 
. 6  * @ClassName SplitMethodTest
 . 7  * @Description
 . 8  * @author AlphaJunS
 . 9  * @date 2019/10/31 21:49
 10  * V1.0 @version
 . 11  * * / 
12 is  public  class SplitMethodTest {
 13 is  
14      @Test
 15      public  void Test () {
 16          // Excel date is 2019/10/12, using the read io stream acquired as "12.10.2019" 
17          String str = "12.10.2019";
 18 is          System.out.println ( "before conversion:" + STR);
 . 19          // . No. to be cut, it is necessary to use a translation character 
20 is          String [] = strArray Split (STR);
 21 is          String [] = strArr arrayReverse (strArray );
 22 is          string dateStr = convertStrArrayToDateStr (strArr);
 23 is          System.out.println ( "date converted string:" + dateStr);
 24      }
 25  
26 is      / ** 
27       * @description cutting string
 28       * @author AlphaJunS
 29       * @date 2019/10/31
 30       * @param[STR]
 31 is       * @return void
 32       * / 
33 is      Private String [] Split (String STR) {
 34 is          String [] = str.split Split ( "\\." );
 35          return Split;
 36      }
 37 [  
38 is      / ** 
39       * @description reverse string array
 40       * @author AlphaJunS
 41 is       * @date 2019/10/31
 42 is       * @param [strArray]
 43 is       * @return java.lang.String []
 44 is       * / 
45      public String[] arrayReverse(String[] strArray) {
46         int length = strArray.length;
47         String temp = "";
48         // 倒序
49         for (int i = 0; i < length/2; i++) {
50             temp = strArray[i];
51             strArray[i] = strArray[length - 1 - i];
52             strArray[length - 1 - i] = temp;
53         }
54         return strArray;
55     }
56 
57     /**
58      * @description 字符串数组转日期字符串
59      * @author AlphaJunS
60      * @date 2019/10/31
61      * @param [strArr]
62      * @return java.lang.String
63      */
64     private String convertStrArrayToDateStr(String[] strArr) {
65         String dateStr = "";
66         int length = strArr.length;
67         for (int i = 0; i < length; i++) {
68             if (i != (length - 1)) {
69                 dateStr += strArr[i] + "-";
70             } else {
71                 dateStr += strArr[i];
72             }
73         }
74         return dateStr;
75     }
76 }

The results are as follows:

 

 Also, if the date is the date string converted, the following code can be used:

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = sdf.parse(dateStr);
            System.out.println("日期:" + date);
        } catch (ParseException e) {
            e.printStackTrace();
        }   

 

Guess you like

Origin www.cnblogs.com/alphajuns/p/11774413.html