Tutorials, Free Online Tutorials, publishbookmarks provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.
Java Written year and month to query the calendar class
package practice;
import java.util.Calendar;
import java.util.Scanner;
import java.util.TimeZone;
/**
* Created by fangjiejie on 2016/11/27.
*/publicclass Mydate {
publicstatic final int dayNum[]={
31,28,31,30,31,30,31,31,30,31,30,31};
static boolean judgeYear(int year){
// Judging whether it is a leap year, it is 29 days in the leap yearif((year%400==0)||((year%4==0)&&(year%100!=0))){
returntrue;}
else{
returnfalse;}
}
staticvoid show(int year,int month,Calendar calendar){
if(judgeYear(year)){
dayNum[1]=29;
}
int count=dayNum[month];// Record the total number of days of the current month
calendar.set(Calendar.YEAR,year);
calendar.set(Calendar.MONTH,month);
calendar.set(Calendar.DAY_OF_MONTH,1);
System.out.println(calendar.getTime());
int p=calendar.get(Calendar.DAY_OF_WEEK);// Calculate the first day of the current month is the first day of the week// This result is calculated on the first day on Sunday,for(int i=0;i<p-1;i++)// The empty number of days can be added with spaces
{
System.out.print(" ");
}
int c=1;// c means one day of the current month, starting from No. 1while(c<=count){
p++;// Use P to control the change of the bank
System.out.print(c+" ");
if(c<10){
System.out.print(" ");// For the alignment of the format, the number of days of one digit outputs the space
}
if(p%7==1){
System.out.println();
}
c++;
}
}
publicstaticvoidmain(String[] args) {
Calendar Md=Calendar.getInstance();
Md.setTimeZone(TimeZone.getTimeZone("GMT+8"));
int year=Md.get(Calendar.YEAR);
int month=Md.get(Calendar.MONTH);
show(year,month,Md);
while(true) {
System.out.println();
System.out.println("If you want to continue to find the calendar of a certain year and a month, then");
Scanner reader = new Scanner(System.in);
System.out.println("Please enter the year:");
year = reader.nextInt();
System.out.println("Please enter the month:");
month = reader.nextInt();
show(year, month - 1, Md);
}
}
}
/***** Formatting Date of Class ****** /
public class FF {
public static void main(String[] args) {
System.out.println(new Date());
SimpleDateFormat l= new SimpleDateFormat("yyyy-MM-dd HH:mm;ss"); // Set the format template
System.out.println(l.format(new Date()));
Calendar c=Calendar.getInstance(); // No need to instantiate the static method creation
c.setTimeZone(TimeZone.getTimeZone("GMT+8"));
System.out.println(c.get(Calendar.YEAR));
System.out.println((c.get(Calendar.MONTH))+1);
System.out.println(c.get(Calendar.DAY_OF_MONTH));
System.out.println(c.get(Calendar.HOUR_OF_DAY));
System.out.println(c.get(Calendar.MINUTE));
System.out.println(c.get(Calendar.SECOND));
c.set(Calendar.YEAR,2016); // Set the value of the calendar field.
c.add(Calendar.YEAR,5); // According to the rules of the calendar, add or subtract the specified amount of time to give a given calendar field.
System.out.println(c.get(Calendar.YEAR)); // Return to the value of the calendar field.
Date m=c.getTime(); // Returns a Date object that means this Calendar time value (from the chant to the current millisecond offset).
System.out.println(l.format(m));
Calendar d=Calendar.getInstance();
d.set(Calendar.YEAR,2004);
d.set(Calendar.MONTH,2);
int k=c.getTime().getDay();
}
}