TestMonth

package  com.jasonhuang.test;

import  java.text.ParseException;
import  java.text.SimpleDateFormat;
import  java.util.Calendar;
import  java.util.Date;

/*
 * 计算指定日期的当月一共几天
 
*/

public   class  TestMonth  {
    
public static void main(String[] args) throws ParseException {
        
// 设置日期表现样式
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        
// 输入的日期
        String str = "2004-02-03";
        
// 对应的Date实例
        Date date = format.parse(str);
        
// 获得Calendar实例
        Calendar calendar = Calendar.getInstance();
        
// 根据date赋值
        calendar.setTime(date);
        
// 计算是当月的第几天
        int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.printf(
"输入的日期是当月的第%d天 ", dayOfMonth);
        
// 计算当月的第一天
        calendar.add(Calendar.DATE, 1 - dayOfMonth);
        System.out.printf(
"当月的第一天是%s ", format.format(calendar.getTime()));
        
// 计算下月的第一天
        calendar.add(Calendar.MONTH, 1);
        System.out.printf(
"下月的第一天是%s ", format.format(calendar.getTime()));
        
// 计算当月的最后一天
        calendar.add(Calendar.DATE, -1);
        System.out.printf(
"当月的最后一天是%s ", format.format(calendar.getTime()));
        
// 计算是当月一共几天
        dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.printf(
"当月一共%d天 ", dayOfMonth);
    }

}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下程序能否编译成功?如果能,写出程序的执行过程和执行结果;如果不能, 说明错误原因并修改代码 1. class StringDemo{ public static void main(String[] args){ String s1 = “a”; String s2 = “b”; show(s1,s2); System.out.println(s1+s2); } public static void show(String s1,String s2){ s1 = s1 +“q”; s2 = s2 + s1; } } 2. public class TestMonth{ public static void main(String []args){ try{ int month=Integer. parseInt("13"); if(month<0||month>12){ throw new ArithmeticException("没有"+month+"月份!"); } System.out.println("您输入的月份为"+month+"月份"); }catch(ArrayIndexOutOfBoundsException e){ System.out.println("请输入月份!"); }catch(ArithmeticException e){ System.out.println("捕获 ArithmeticException 异常"); System.out.println(e.toString()); } } } 3. class Test{ public void stringReplace(String text){ text= text.replace('j','i'); text=text+"C"; } public void bufferReplace(StringBuffer text){ text.setCharAt(0,'i'); text=text.append("C"); } public void change(char ch[]) { ch[0]='Y'; } public static void main(String args[]){ String str1="java"; StringBuffer str2=new StringBuffer("java"); char ch[]={'j','a','v','a'}; Test t=new Test(); t.change(ch); t.stringReplace(str1); t.bufferReplace(str2); System.out.println(str1); System.out.println(str2.toString()); System.out.println (new String(ch)); } } 4. class Test{ public static String stringReplace(String text){ text=text.replace('j','i'); text=text+"C"; return text; } public static StringBuffer bufferReplace(StringBuffer text){ text.setCharAt(0,'i'); text= text.append("C"); return text; } public static void main(String aargs[]){ String str1="java"; String str2="java"; String str3="j"+"a"+"v"+"a"; String str4=new String("java"); StringBuffer str5=new StringBuffer("java"); System.out.println(str1==str2); System.out.println(str2==str3); System.out.println(str2==str4); System.out.println(str3.equals(str4)); System.out.println(str4.equals(str5)); System.out.println (stringReplace(str1)); System.out.println(bufferReplace(str5)); System.out.println(str1); System.out.println(str5); } } 5. interface Shape{ double PI; public double area(); public double perimeter(); } class Cycle extends Shape{ private double r; public Cycle(double r){ this.r=r; } double area(){ System.out.println(PI*r*r); } } public class Test{ public static void main(String args[]){ Cycle c=new Cycle(1.5); System.out.println("面积为:"+c.area()); } }
最新发布
05-26

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值