Java Acquisition Time

Source: Internet
Author: User
     One: Get the current time, an hour ago, an hour later, January ago, the time after January
 
      GregorianCalendar a = new GregorianCalendar ();
      SimpleDateFormat format = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");

      System.out.println ("Current Time:" +format.format (A.gettime ()));

      A.add (Calendar.hour_of_day,-1);
      System.out.println ("An hour ago:" +format.format (A.gettime ()));

      A = new GregorianCalendar ();
      A.add (Calendar.day_of_month,-1);
      System.out.println ("One day ago:" +format.format (A.gettime ()));
 
      A = new GregorianCalendar ();
      A.add (Calendar.month,-1);
      System.out.println ("Previous month:" +format.format (A.gettime ()));

     A = new GregorianCalendar ();
     A.add (Calendar.month, 1);
     System.out.println ("After January:" +format.format (A.gettime ()));

     A = new GregorianCalendar ();
     A.add (calendar.hour_of_day,5);
     

 
Two: Get the current month maximum days
  
  Calendar cal = Calendar.getinstance ();
  int Day=cal.getactualmaximum (calendar.day_of_month);
  SYSTEM.OUT.PRINTLN ("Maximum days of the current month:" +day);


  Three: Format date public
  
  static Date parsedate (string datestr, string format) {
		date date = null;
		try {
			SimpleDateFormat format2 = new SimpleDateFormat (format);
			Date = Format2.parse (DATESTR);
		} catch (Exception e) {
		} return
		date;
	}

  Four: Date turns string public
  
  static string format (date date, String format) {
		string result = "";
		try {
			if (date!= null) {
				SimpleDateFormat DateFormat = new SimpleDateFormat (format);
				result = Dateformat.format (date);
			}
		catch (Exception e) {
		} return result
		;
	}

  Five: Return this time is the number of weeks of this year public
  
  static int Getyearofweek (date date) {
		Calendar calendar = calendar.getinstance ();
		Calendar.settime (date);
		Return Calendar.get (calendar.week_of_year);
	}

  VI: Judge whether two dates are not one week public

  static Boolean weekofyeak (date date1, date date2) {
		String dates1 = Getmondayofthisweek ( DATE1);
		String dates2 = Getmondayofthisweek (date2);
		if (Dates1.equals (Dates2)) {return
			true;
		}
		return false;
	}

  Vii. return this time is the number of days of the year public
	
  static int getyearofday (date date) {
		Calendar calendar = calendar.getinstance ();
		Calendar.settime (date);
		Return Calendar.get (calendar.day_of_year);
	}

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.