Conversion of strings and dates in java

Source: Internet
Author: User
Tags dateformat

Copy codeThe Code is as follows: import java. SQL. Timestamp;
Import java. text. DateFormat;

Import java. text. ParseException;

Import java. text. SimpleDateFormat;

Import java. util. Date;

Public class DateIO {

Public static void main (String [] args ){

Date date = new DateIO (). strToDate ("2013-04-01 ");

String strdate = new DateIO (). dateToStr (date );

String srrdate = new DateIO (). timestampToStr (System. currentTimeMillis ());

Timestamp ts = new DateIO (). strToTimestamp (new Date ());

}

// Convert String to Date

Public Date strToDate (String strdate ){

DateFormat format = new SimpleDateFormat ("yyyy-MM-dd ");

Date date = null;

Try {

Date = format. parse (strdate );

} Catch (ParseException e ){

E. printStackTrace ();

}

System. out. println ("date:" + date );

Return date;

}

// Convert Date to String

Public String dateToStr (Date date ){

// Year, month, and day ****-**-**

DateFormat format = new SimpleDateFormat ("yyyy-MM-dd ");

String str = format. format (date );

System. out. println ("str:" + str );

// Year, month, and day **-*-*

Format = DateFormat. getDateInstance (DateFormat. SHORT );

Str = format. format (date );

System. out. println (str );

// Year, month, and day ****-*-*

Format = DateFormat. getDateInstance (DateFormat. MEDIUM );

Str = format. format (date );

System. out. println (str );

// ***** Month day week *

Format = DateFormat. getDateInstance (DateFormat. FULL );

Str = format. format (date );

System. out. println (str );

Return str;

}

// Convert Timestamp to String

Public String timestampToStr (Long timestamp ){

SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); // defines the format, not in milliseconds

String str = df. format (timestamp );

System. out. println (str );

Return str;

}

// Convert Date to Timestamp

Public Timestamp strToTimestamp (Date date ){

SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss ");

String time = df. format (date );

Timestamp ts = Timestamp. valueOf (time );

System. out. println (ts );

Return ts;

}

}

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.