Java gets the method of yesterday's date string _java

Source: Internet
Author: User

The example in this article describes Java's method for getting yesterday's date string. Share to everyone for your reference, specific as follows:

Import Java.text.SimpleDateFormat;
Import Java.util.Date; public class Test {/** * gets yesterday's date string * * @param nowdate * Current date String * @return/public static S
    Tring Getlastdayinfo (String nowdate) {String yesterday = "";
    int year = 0;
    int month = 0;
    int day = 0;
      try {year = Integer.parseint (nowdate.substring (0, Nowdate.indexof ("-")));
      month = Integer.parseint (nowdate.substring (Nowdate.indexof ("-") + 1, Nowdate.lastindexof ("-)"));
      Day = Integer.parseint (nowdate. Substring (Nowdate.lastindexof ("-") + 1));
      System.out.println ("nowyear =" + year);
      System.out.println ("Nowmonth =" + month);
      System.out.println ("Dayday =" + day);
      Day = day-1;
        if (day = = 0) {month = month-1;
          if (month = = 0) {//January month = 12;
          Day = 31;
        Year = year-1; else {//not.
          Switch (month) {//1|3|5|7|8|
          10|12) day=31;;
            Case 1:day = 31;
          Break
            Case 3:day = 31;
          Break
            Case 5:day = 31;
          Break
            Case 7:day = 31;
          Break
            Case 8:day = 31;
          Break
            Case 10:day = 31;
          Break
            Case 12:day = 31;
          Break
          4|6|9|11) day=30;;
            Case 4:day = 30;
          Break
            Case 6:day = 30;
          Break
            Case 9:day = 30;
          Break
            Case 11:day = 30;
          Break Case 2:if (year% 4 = 0 && Year%!= 0 | | year% = = 0) {//Leap y
            Ear day = 29;
          else Day = 28;
  }
        }    } String monthstr = "";
      String daystr = "";
      if (Month <) {Monthstr = "0" + string.valueof (month);
      else {monthstr = string.valueof (month);
      } if (Day <) {Daystr = "0" + string.valueof (day);
      else {daystr = string.valueof (day);
    } yesterday = string.valueof (year) + "-" + Monthstr + "-" + daystr;
    catch (Exception e) {e.printstacktrace ();
  return yesterday;
    public static void Main (string[] args) throws Exception {System.out.println ("2008-03-01");
    System.out.println (Getlastdayinfo ("2008-01-01"));
    SimpleDateFormat SDF = new SimpleDateFormat ("mmmm-dd-yyyy");
    Date date;
    String datetime = "02-14-1984";
      try {date = Sdf.parse (datetime);
    System.out.println (Sdf.format (date));
    catch (Exception e) {System.out.println (E.getmessage ()); } Date D =Java.sql.Date.valueOf ("2008-07-06");
  System.out.println (D.tostring ());

 }
}

More about Java-related content readers can view the site topics: "Java files and directory operation tips Summary", "Java Data structure and algorithm tutorial", "Java Operation DOM node skills summary" and "Java Cache Operation Tips Summary"

I hope this article will help you with Java programming.

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.