Conversion of dates between databases and pages in Java

Source: Internet
Author: User

This is the use and conversion of dates that are frequently used in common projects:

Import Java. util. date; import Java. text. parseexception; import Java. text. simpledateformat;/*** set Java. util. the date type is exchanged with the string type, and the date format * @ author administrator **/public class convertdate {/*** is specified to convert the date to the string: yyyy-mm-dd hh: mm: SS * @ Param date * @ return Str */public static string datetostr (date) {simpledateformat format = new simpledateformat ("yyyy-mm-dd hh: mm: SS "); string STR = format. format (date); Return STR;}/*** convert a date to a string: yyyy-mm-dd * @ Param date * @ return */public static string datetosimplestr (date) {simpledateformat format = new simpledateformat ("yyyy-mm-dd"); string STR = format. format (date); Return STR;}/*** string to date * @ Param Str * @ return date */public static date strtodate (string Str) {simpledateformat format = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); Date = NULL; try {date = format. parse (STR);} catch (parseexception e) {e. printstacktrace ();} return date;}/*** Java. SQL. date and Java. util. date type conversion * @ Param sdate * @ return */public static Java. util. date sqldatetoutildate (Java. SQL. date sdate) {Java. util. date udate = NULL; long T = sdate. gettime (); udate = new date (t); Return udate;} public static Java. SQL. date utildatetosqldate (Java. util. date udate) {Java. SQL. date sdate = NULL; long T = udate. gettime (); sdate = new Java. SQL. date (t); Return sdate;} public static void main (string [] ARGs) {date = new date (); system. out. println ("date to string:" + convertdate. datetostr (date); system. out. println ("string to date:" + convertdate. strtodate (convertdate. datetostr (date )));}}

 

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.