"Java" is more than 1 days apart from two times in a string representation

Source: Internet
Author: User

The date the database is stored, even if it is represented by a string, you can also use "Mysql" to find the most recent difference x days, the SQL statement on the date of the comparison (click on the link) described in the Timestampdiff function, the date is not more than one day.

If the date in Java is equal to two of the dates represented as a string,

For example, the comparison of the two with "Yyyy-mm-dd HH:mm:ss" and string form the date, is equal

That can first remove the "-", ":", "" "space, etc., intercept the first 8 strings, that is," YYYY-MM-DD "part, then get a 8-bit long shape, two date subtraction can be.

The following procedure determines whether the "2019-09-06 16:04:07" and the current date "2015-5-7" are the same day.

Package Timedifftest;import java.text.simpledateformat;import java.util.date;class util {public static Boolean Stringtimedaydiff (String stringTime1, String stringTime2) {Long dateTime1 = Long.parselong (Stringtime1.replaceall ("-" , ""). ReplaceAll (":", ""). Replace ("", ""). Substring (0, 8)); Long dateTime2 = Long.parselong (Stringtime2.replaceall ("-", ""). ReplaceAll (":", ""). Replace ("", ""). Substring (0, 8)); return (dateTime1 = = dateTime2)? True:false;}} public class Timediff {public static void main (string[] args) {String dbTime = "2019-09-06 16:04:07"; String nowtime = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format (New Date (System.currenttimemillis ())); System.out.println (Util.stringtimedaydiff (Dbtime,nowtime));}}
The results of the operation are as follows:


"Java" is more than 1 days apart from two times in a string representation

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.