Java special objects and their Applications 2 (time calculation)

Source: Internet
Author: User



The/*calendar class is an abstract class that provides methods for converting a particular moment to a set of calendar fields such as year, MONTH, Day_of_month, HOUR, and so on.
and provides some methods for manipulating calendar fields, such as getting the date of the next week.
The instantaneous value is expressed as a millisecond, which is the offset from the calendar element (that is, Greenwich Mean Time January 1, 1970, 00:00:00.000, Gregorian calendar).
*/




Import Java.util.Calendar;
/*
Practice
* 1. Get the number of days in February of any year.
* 2. Get the present moment of yesterday.
*/
public class Calendardemo {
public static void Main (string[] args) {
Gets the current time object
Calendar c=calendar.getinstance ();
Increment or decrement a specified time, offset by two times
C.add (calendar.day_of_month,-1);
Outputs the specified time value
SOP (C.get (Calendar.day_of_month) + "Day:" +c.get (Calendar.hour_of_day) + ":" +c.get (Calendar.minute) + ":" +c.get ( Calendar.second));
SOP (c);
Custom Time, hour_of_day=12,minute=20,second=38
C.set (2012,2,23);
Increase or decrease time, offset of time two
C.add (CALENDAR.MONTH,-12);
Printcalendar (c);
Sumcalendar (c,2012);
}

public static void Sumcalendar (Calendar c,int year)
{
C.set (year,2,1);
C.add (calendar.day_of_month,-1);
SOP (C.get (calendar.day_of_month));
}
public static void Printcalendar (Calendar c) {

Get the month by looking up the table and not add 1.
String mon[]={"January", "February", "March", "April", "May", "June", "July"
, "August", "September", "October", "November", "December"};
Since the week starts at 1, define an empty string for 0.
String weeks[]={"", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
"Saturday"};
Get time Information Object
int Index=c.get (calendar.month);
int Index1=c.get (Calendar.day_of_week);
Get year
SOP (C.get (calendar.year) + "year");
Gets the month, because the method starts with 0, so the month is calculated by adding a
// SOP ((C.get (calendar.month) +1) + "month");
SOP (Mon[index]);
Get Day
SOP (C.get (Calendar.day_of_month) + "Day");
Get Week
SOP ("Week" +c.get (Calendar.day_of_week));
SOP (Weeks[index1]);


}
Public static<t> void sop (T t)
{
System.out.println (t);
}
}

Import java.util.*;

import java.text.*;
public class Datedemo {
public static void Main (string[] args) {
//Gets the current time.
date D=new date ();
SOP (d);
//Set time mode
simpledateformat SDF =new SimpleDateFormat ("yyyy mm month DD Day e hh:mm:ss");
//Get the current time and set the mode output
String time= Sdf.format (d);
SOP ("Current time is:" +time);
}
public static<t> void SOP (T T)
{
System.out.println (t );
}
}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java special objects and their Applications 2 (time calculation)

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.