Orcale date functions to_date () and to_char (), orcaleto_date

Source: Internet
Author: User

Orcale date functions to_date () and to_char (), orcaleto_date
The two functions of date conversion are to_date () and to_char (). The to_date () function converts the character type to the date type in a certain format, and to_char () convert a date to a certain format to a character type

When the time needs to be accurate, it is best to use to_char () to compare with character types. Comparison methods (=, >=, <=, between and) <,> need to be escaped, they are <>

To_date:

To_date ('1970-01-20 ', 'yyyy-mm-dd'). The former is a string, and the string date is converted to the date format specified by the input format template.

Note that when the converted date format includes time, minute, and second: to_date ('2017-01-20 12:34:56 ', 'yyyy-mm-dd hh24: mi: ss '), the template has HH, HH12 (in 12-hour format, the time range is: 0:00:00-23:59:59) HH24 :() in 24-hour format, the value is 0:00:00-23:59:59)

To_char:

To_char (dateType, 'yyyy-mm-dd hh24: mi: ss') converts the date dateType in the format of date to the year, month, and day of the character type.

Query the number of weeks for a given date:

Select to_char (to_date ('1970-01-20 ', 'yyyy-mm-dd'), 'day') from dual; // Saturday

Obtain the date seven days before the given time:

String createTime = "2018-01-20" Calendar before7day = Calendar. getInstance ();

Date date = null; try {

Date = new SimpleDateFormat ("yy-MM-dd"). parse (createTime );

Before7day. setTime (date );

Int day = before7day. get (Calendar. DATE );

Before7day. set (Calendar. DATE, day-7 );

String dayBefore = new SimpleDateFormat ("yyyy-MM-dd"). format (before7day. getTime ();} catch (ParseException 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.