String date to the specified format of the date stringparsedate

Source: Internet
Author: User

/**
* Convert string dates to dates in the specified format
* @ Param Str
* @ Return
* @ Throws parseexception
*
* @ Author zhaozj
*/
Public static date stringparsedate (string STR, string format) throws parseexception {

String strformat = constants. GetType (STR );

Simpledateformat SDF = new simpledateformat (strformat );
Simpledateformat sdf1 = new simpledateformat (format );

If (STR = NULL | "". Equals (STR )){
Return NULL;
} Else {
Date = SDF. parse (STR );
String strdate = sdf1.format (date );
Return sdf1.parse (strdate );
}
}

 

// Get date format based on string date

Public static string GetType (string date ){
String result = NULL;
If (date. Matches ("\ D {4}-\ D {2}-\ D {2}
\ D {2 }:\\ d {2 }:\\ d {2}. \ D + ")){
Result = constants. date_model_1;
} Else if (date. Matches ("\ D {4}-\ D {2}-\ D {2}
\ D {2 }:\\ d {2 }:\\ d {2 }")){
Result = constants. date_model_2;
} Else if (date. Matches ("\ D {4}-\ D {2}-\ D {2}
\ D {2 }:\\ d {2 }")){
Result = constants. date_model_3;
} Else if (date. Matches ("\ D {4}-\ D {2}-\ D {2}
\ D {2 }")){
Result = constants. date_model_4;
} Else if (date. Matches ("\ D {4}-\ D {2}-\ D {2 }")){
Result = constants. date_model_5;
} Else if (date. Matches ("\ D {4}-\ D {2 }")){
Result = constants. date_model_6;
} Else if (date. Matches ("\ D {14}. \ D + ")){
Result = constants. date_model_7;
} Else if (date. Matches ("\ D {14 }")){
Result = constants. date_model_8;
} Else if (date. Matches ("\ D {12 }")){
Result = constants. date_model_9;
} Else if (date. Matches ("\ D {10 }")){
Result = constants. date_model_10;
} Else if (date. Matches ("\ D {8 }")){
Result = constants. date_model_11;
} Else if (date. Matches ("\ D {6 }")){
Result = constants. date_model_12;
} Else if (date. Matches ("\ D {4}/\ D {2}/\ D {2}
\ D {2 }:\\ d {2 }:\\ d {2}. \ D + ")){
Result = constants. date_model_13;
} Else if (date. Matches ("\ D {4}/\ D {2}/\ D {2}
\ D {2 }:\\ d {2 }:\\ d {2 }")){
Result = constants. date_model_14;
} Else if (date. Matches ("\ D {4}/\ D {2}/\ D {2}
\ D {2 }:\\ d {2 }")){
Result = constants. date_model_15;
} Else if (date. Matches ("\ D {4}/\ D {2}/\ D {2}
\ D {2 }")){
Result = constants. date_model_16;
} Else if (date. Matches ("\ D {4}/\ D {2}/\ D {2 }")){
Result = constants. date_model_17;
} Else if (date. Matches ("\ D {4}/\ D {2 }")){
Result = constants. date_model_18;
}
Return result;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.