Java About Time Operation

xiaoxiao2021-03-06  19

In the nearest OA, I need to judge whether the two dates are the same week, obtaining the weekly Monday and Friday date according to a given date.

When completing the above tasks, I found that Calendar is indeed a powerful Class.

The source code is given below, and the brothers refer to the reference.

/ * * Create Date 2005-3-30Package Com.infoeArth;

Import java.sql.timestamp; import java.text. *; import java.util. *; / ** * @ 李春 * public class manageweek {// Judgment two dates in the same week Boolean Issameweekdates (Date Date1, Date Date2 ) {Calendar Cal1 = Calendar.GetInstance (); Calendar Cal2 = Calendar.GetInstance (); Cal1.SetTime (Date1); Cal2.SetTime (Date2); int subyear = Cal1.Get (Calendar.Year) - Cal2.Get ( Calendar.year); if (0 == Subyear) {if (CAL1.GET (Calendar.Week_OF_Year) == Cal2.Get (Calendar.Week_of_year) Return True;} else if (1 == Subyear && 11 == CAL2 .get (calendar.month)) {// If the last week of December, the last week of the coming year, the last week is the first week of the first week of the year (CAL1.GET (Calendar.Week_Of_Year) == Cal2.Get (Calendar.Week_of_year) Return True;} else if (-1 == Subyear && 11 == Cal1.Get (Calendar.mont)) {if (CAL1.GET (Calendar.Week_OF_YEAR) == Cal2.Get (Calendar. WEEK_OF_YEAR)) return true;} return false;} // circumferential sequence generated public static String getSeqWeek () {Calendar c = Calendar.getInstance (Locale.CHINA); String week = Integer.toString (c.get (Calendar.WEEK_OF_YEAR) ); If Week.Length () == 1) Week = "0" week; string year = integer.tostring (c.Get (Calendar.Year)); Return Year Week;} // Get a date of Monday public static string getMonday (Date Date) {Calendar C = Calendar.GetInstance (); C.SetTime (Date); C.SET (Calendar.day_of_week, calendar.monday); Return New SimpleDateFormat ("YYYY-MM-DD"). Format (C .gettime ());} // Get the date of Friday public static string getfriday (Date Date) {Calendar C = Calendar.GetInstance (); C.SetTime (Date); C.SET (Calendar.day_of_week, Calendar.friday Return New SimpleDateFormat ("YYYY-MM-DD"). Format (c.gettime ());}

// Date of the date of the current date or after a few days public static string instildAy (int N) {Calendar C = Calendar.GetInstance (); DATEFORMAT DF = New SimpleDateFormat ("YYYY-MM-DD"); C.SETTIME New Date ()); C.Add (Calendar.date, N); DATE D2 = C.GetTime (); string s = df.format (d2); return s;} public static void main (String [] args {

}

}

I often encounter a problem that turns a string into a date, in fact, 3 sentences:

String mystring = "2005/12/20";

SimpleDateFormat SDF = New SimpleDateFormat ("YYYY / MM / DD", Locale.china); DATE D = SDF.PARSE (MyString);

Here is an example, archive

Package formatdatetime;

Import java.text. *; import java.util. *;

/ ** * @Author administrator * * Todo To change the template for this generated type annotation, go to * Window - Preference - Java - Code Style - Code Template * / Public Class StringTodateTimetest {

Public static void main (String [] args) {

String mystring = "Tue Oct 18 04:11:56 CST 2005"; // String mystring = "2005/12/20"; try {simpledateformat sdf = new SimpleDateFormat ("Eee MMM DD HH: MM: SS ZZZ YYYY", Locale.us); // SimpleDateFormat SDF = New SimpleDateFormat ("YYYY / MM / DD", locale.china); DATE D = SDF.PARSE (MyString); Calendar C = Calendar.GetInstance (); C.SetTime (D ); String s = new SimpleDateFormat ("YYYY-MM-DD"). Format (C.getTime ()); system.out.println (s);} catch (exception e) {E.PrintStackTrace ();}} }

转载请注明原文地址:https://www.9cbs.com/read-40063.html

New Post(0)