Orcale date function to_date (), 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, & lt; & gt;
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();
}