C # string conversion to date time format and its error handling

Source: Internet
Author: User
Tags dateformat

Article keywords: C # string conversion to date C # Date conversion string string conversion date string convert to date integer convert to string floating-point number convert to string literal to time convert string to time character conversion to string string convert to time object character string conversion to date type string to date

√ string conversion to date type (C #)

such as: "20100101" converted to date type?

"20100101" converted to int type How to convert??

1, DateTime dt=convert.todatetime ("20100101"). Substring (0,4) + "-" + "20100101". Substring (4,2) + "-" + "20071107". Substring (6,2));
int I=convert.toint32 ("20100101");

2, Convert.todatetime, DateTime.Parse ()

3, String str = "20100101";
DateTime dt = datetime.parseexact (str, "YYYYMMDD", null);
int i;
Int. TryParse (str, out i);

4, define a DateTimePicker object, and then assign the string that needs to be transformed to the Text property of the DateTimePicker object,
Then the value of the DateTimePicker object is the date and time you need, the value and Minite,second, and so on, you can get
Time, minutes, seconds, and Hao second equivalent.

√java string to date time format

Date time format: Yyyy-mm-dd hh:mm:ss
String time = "1900-02-21 12:23:33";
Converts a string to a date and time
SimpleDateFormat DateFormat = new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");
Date and time of the build
Date date = DateFormat. Parse (time);

Date is what we want.

If there are 2 or more places in your program that call the Dateformat.parse () method, you may get an error: Unhandled exception type ParseException.

To do this, you need to use the Try-catch block to capture:

try{
            DateFormat. Parse (time);
         } catch (Exception e) {
             E.printstacktrace ();
        } 

Related Article

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.