I want to get the current date, then add a certain integer, get the next date, I found that the Date class is not easy to use during the test, today is 2005-01-14, I want to get the date before 28 days, I Write a method: // Incontinent time, add or subtimate from the current date, get a new date // public static string strandateByint (int i, string operator) {// long j = 0; / / Long tmp; /// DATE D = New Date (); /// TMP = (long) (D.getTime () / 24 * 3600 * 1000); // // if ("-". Equals (Operator)) {// j = d.gettime () - i * 24 * 3600 * 1000; ///} else if (" ". Equals (Operator)) {// j = d.gettime () I * 24 * 3600 * 1000; //} // Date a = new date (j); //// SimpleDateFormat SDF = New SimpleDateFormat ("YYYY-MM-DD"); /// // system. Out.println (SDF.Format (a)); // Return SDF.Format (a); ////} Result When I call this method, the result is actually 2005-02-07, and this value is The results between 25-51 are not right (thanks to serious testers!). No, I check some information, rewrite the method as follows: Public Static String StrtodateByint (INT I, STRING OPERATOR) {long j = 0;
Date d = new date (); // system.out.println ("y =" D.Getyear () 1900); // system.out.println ("m =" D.getMonth ()); // system.out.println ("day =" d.getday ()); // system.out.println ("DATE =" D.Getdate ()); GregorianCalendar gc = new gregoriancalendar (); GC. Settime (d); system.out.println (D.getTime ()); if (" ". Equals (operator)) {gc.add (GregorianCalendar.date, i);} else if ("-". Equals (operator) {gc.add (Gregoriancalendar.date, (-i));} DATE A = gc.gettime (); SimpleDateFormat SDF = New SimpleDateFormat ("YYYY-MM-DD"); Return SDF.Format (A );