2013年11月17日星期日

Please help me to see one of this calendar output program , the output bit of a problem , I do not know how to change

Output format is a little wrong, check a long time, I do not know where the problem lies , please help.

import java.text.DateFormatSymbols;
import java.util.*;

public class Test
{
public static void main(String[] args)
{

String[] weekdayNames = new DateFormatSymbols().getWeekdays();
for(int i=1; i<=7; i++)
{
  System.out.print(weekdayNames[i]);
  System.out.print(" ");
  }
  System.out.println();
  
  
  GregorianCalendar calendar = new GregorianCalendar();
  int firstDayOfWeek = calendar.getFirstDayOfWeek();
  calendar.set(Calendar.DAY_OF_MONTH, 1);
  int weekday = calendar.get(Calendar.DAY_OF_WEEK);
   int blank = 0;
   while(weekday != firstDayOfWeek)
   {
    blank++;
    calendar.add(Calendar.DAY_OF_MONTH,-1);
    weekday = calendar.get(Calendar.DAY_OF_MONTH);    
   }
   
   for(int i=0; i<blank; i++)
   {
    System.out.print(" ");
   }
   
   int month = calendar.get(Calendar.MONTH);
   calendar.set(Calendar.DAY_OF_MONTH, 1);
   int day = calendar.get(Calendar.DAY_OF_MONTH);
   System.out.printf("%7d", day);
   calendar.add(Calendar.DAY_OF_MONTH, 1);
   while(calendar.get(Calendar.MONTH) == month)
   {                
    weekday = calendar.get(Calendar.DAY_OF_WEEK);
    if(weekday == firstDayOfWeek)
    {
    System.out.println();
    }
    day = calendar.get(Calendar.DAY_OF_MONTH);    
    System.out.printf("%7d", day);
    calendar.add(Calendar.DAY_OF_MONTH, 1);        
   }    
}
}


my program output is so



but it should be transferred in the next line when the 3rd fishes , but did not change jobs, I do not know why, of course, is certainly due weekday == firstDayOfWeek not established , but my program should be the 3rd time weekday == firstDayOfWeek set up properly, I do not know why not set up , did not go to the next line ?

existing is November , the computer displays the arrangement should be

So I do not know why the program arranged incorrect ?
------ Solution ---------------------------------------- ----


operation above you change the value of your calendar to get the month is not November , and should be able to reset the next .

calendar.setTime (new Date ());
int month = calendar.get (Calendar.MONTH);
calendar.set (Calendar.DAY_OF_MONTH, 1);
------ For reference only --------------- ------------------------
why the picture does not show , Zaifayici ,

This is my own output.
------ For reference only -------------------------------------- -
no attention to me ? This is the first time I encountered such a long time did not respond to me.
------ For reference only -------------------------------------- -
really is the month is wrong. I changed a bit , you can output the correct form .

import java.text.DateFormatSymbols;
import java.util.*;

public class Test
{
public static void main(String[] args)
{

String[] weekdayNames = new DateFormatSymbols().getWeekdays();
for(int i=1; i<=7; i++)
{
  System.out.print(weekdayNames[i]);
  System.out.print(" ");
  }
  System.out.println();
     
  GregorianCalendar calendar = new GregorianCalendar();
      
  int firstDayOfWeek = calendar.getFirstDayOfWeek();
  int month = calendar.get(Calendar.MONTH);
  
  calendar.set(Calendar.DAY_OF_MONTH, 1);
  int weekday = calendar.get(Calendar.DAY_OF_WEEK);

   int blank = 0;
   while(weekday != 1)
   {
    blank++;
    calendar.add(Calendar.DAY_OF_MONTH,-1);
    weekday = calendar.get(Calendar.DAY_OF_MONTH);    
   }
   
   for(int i=0; i<blank; i++)
   {
    System.out.print(" ");
   }    

   calendar.set(Calendar.MONTH, 10);     //此处是与第一次贴出的程序不同之处。    
   calendar.set(Calendar.DAY_OF_MONTH, 1);
       
   int day = calendar.get(Calendar.DAY_OF_MONTH);
   System.out.printf("%7d", day);
calendar.add(Calendar.DAY_OF_MONTH, 1);
   while(calendar.get(Calendar.MONTH) == month)
   {                
    weekday = calendar.get(Calendar.DAY_OF_WEEK);
    if(weekday == firstDayOfWeek)
    {
    System.out.println();
    }
    day = calendar.get(Calendar.DAY_OF_MONTH);    
    System.out.printf("%6d", day);
    calendar.add(Calendar.DAY_OF_MONTH, 1);        
   }    
}
}


correct output in the form of





没有评论:

发表评论