java 核心技术 第四章 Calendar 测试 与Calendar 的使用EmployeeTest

深入理解 JVM 原理
深入理解 JVM 原理
wangxiaoming

                                          java 核心技术 第四章内容

题目大意很简单就是打印出日历呗:

先看 代码:

import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.GregorianCalendar;


public class CalendarTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//程序供能实现日历显示
		//问题。日历程序包含很多问题例如那一天是星期几,每个月有多少天有咯GregorianCalendar 
		//类那么就可以简单了
		
		//新建类的一个对象
		GregorianCalendar d = new GregorianCalendar();
		
		//今天是第几天  那个月 其中天要使用 day of month 进行
		int today  = d.get(Calendar.DAY_OF_MONTH);
		int month  = d.get(Calendar.MONTH);
		
		
		//设置d to start the day of month 
		//这个意思不明朗
		d.set(Calendar.DAY_OF_MONTH, 1);
		
		//获得这个月的星期几
		
		int weekday = d.get(Calendar.DAY_OF_WEEK);
		
	     //get the firstdayof week 
		  int firstDayOfWeek = d.getFirstDayOfWeek();
		  
		  int intent =  0 ;
		  while(weekday != firstDayOfWeek)
		  {
			  intent ++ ;
			  d.add(Calendar.DAY_OF_MONTH, -1);
			  weekday = d.get(Calendar.DAY_OF_WEEK);
			  
		  }
		   //print week day  nae 
		  String [] weekdayname = new DateFormatSymbols().getShortWeekdays();
		  do{
			  System.out.printf("%4s", weekdayname[weekday]);
			  d.add(Calendar.DAY_OF_MONTH, 1);
			  weekday = d.get(Calendar.DAY_OF_WEEK);
			  
		  }while(weekday != firstDayOfWeek);
		  
		  System.out.println(" ");
		  
		  for(int i = 1; i<intent ; i++)
		  {
			  System.out.print(" ");
		  }
		  d.set(Calendar.DAY_OF_MONTH, 1);
		  do{
			  int day = d.get(Calendar.DAY_OF_MONTH);
			  System.out.printf("%3d", day) ;
			  if(day == today ) System.out.print("*");
			  else System.out.print(" ");
			  
			  d.add(Calendar.DAY_OF_MONTH, 1);
			  weekday = d.get(Calendar.DAY_OF_WEEK);
			  
			  if(weekday == firstDayOfWeek) System.out.println();
			  
			  
		  }while(d.get(Calendar.DAY_OF_MONTH) == month);
		  if( weekday != firstDayOfWeek) System.out.println();
	}

}


下面是EmpolyeeTest.java 代码:

步骤: 新建一个Employee 类,该类中包 含有员工的基本信息,与及对员工的操作函数

               在主类中新建一个Employee 类的操作数组对象 分别给对象赋值 

               对员工进行加薪 ,打印输出 代码要注意的是 for( Employee e : stuff) 这个的应用在c 中没有的

      代码:

import java.util.Date;
import java.util.GregorianCalendar;


public class EmployeeTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub 
		//新建一对象
		Employee[] stuff = new  Employee[3];
		
		stuff[0] = new Employee("Harry", 46787, 1985, 4, 13);
		stuff[1] = new Employee("Jane", 2454, 1993, 5, 10);
		stuff[2] = new Employee("KangKang",99224, 1993,3,31);
		//每个人的工资都提高5%
		
        for(Employee e : stuff )
        {
        	e.raiseSalary(5);
        }
        for(Employee e : stuff)
        {
        	System.out.println("name :" + e.getName()+ "  Salary :" + e.getSalary()+" HireDay :" + e.getHireday());
        }

	}
}
class Employee 
{//新建一个Employee类 这里报含员工所有的参数
	public  Employee(String n , double s , int year , int month , int day )
	{
		 name = n ;
		 salary  = s;
		 //新建一个日历用来存储员工雇佣的时间
		 GregorianCalendar calendar  = new GregorianCalendar (year, month-1 , day);
		 hireday = calendar.getTime();
		 
	}
	public String getName()
	{
		return name ;
	}
	public double getSalary()
	{
		return salary;
	}
	public Date getHireday()
	{
		return hireday;
	}

	public void raiseSalary(double byPercent)
	{
		double raise = salary* byPercent/100;
		salary += raise ;
	}
	private  String name;
	private  double salary;
	private Date hireday;
}

运行结果如下:

name :Harry  Salary :49126.35 HireDay :Sat Apr 13 00:00:00 CST 1985
name :Jane  Salary :2576.7 HireDay :Mon May 10 00:00:00 CST 1993
name :KangKang  Salary :104185.2 HireDay :Wed Mar 31 00:00:00 CST 1993



wangxiaoming CSDN认证博客专家 架构 Spring Boot Redis
博客是很好的总结和记录工具,如果有问题,来不及回复,关注微信公众号:程序员开发者社区,获取我的联系方式,向我提问,也可以给我发送邮件,联系 1275801617@qq.com
©️2020 CSDN 皮肤主题: Age of Ai 设计师: meimeiellie 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值