真正实现:输出给定日期的下一天

package sun.date;
import java.util.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;


public class GetNextDate { 
 //思路:类之间的系列转换:
string->SimpleDateFormat->Date->Calendar->Calendar-//>Date>SimpleDateFormat->String
 
 //接收输入的String类型表示的日期inputdate
 public String getDate(String inputdate) {   
        Date date=null;
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        try
        {   
        //把String类型表示的日期转换为Date对象
        date=format.parse(inputdate);
        }catch(ParseException e){
        e.printStackTrace();
        } 
        //getInstance() 使用默认时区和语言环境获得一个日历。
        Calendar nextDate = Calendar.getInstance();
        // void setTime(Date date) 使用给定的 Date 设置此 Calendar 的时间。
        nextDate.setTime(date);
        //根据日历的规则,为给定的日历字段添加或减去指定的时间量。
       //static int  Calendar.DAY_OF_MONTH     指出一个月中的某天
       nextDate.add(Calendar.DAY_OF_MONTH, 1);  

//Date getTime(); 返回一个表示此 Calendar 时间值(从历元至现在的毫秒偏移量)的Date 对象。
      date = nextDate.getTime(); //下一天
      SimpleDateFormat f2=null;
      f2 = new SimpleDateFormat("yyyy-MM-dd");
      //把日期格式化
      String Str = f2.format(date);
      return Str;
 }

 public static void main(String[] args){
       System.out.println("程序功能:输入yyyy-mm-dd格式的日期,返回该日期的下一天。/n退出:end");
       Scanner input = new Scanner(System.in);
       String str = input.next();
       GetNextDate nextday = new GetNextDate();
       while(!str.equals("end"))
       {
       // 调用方法生成新的日期,即输入日期的下一天
       String newDate=nextday.getDate(str);
       System.out.println(newDate);
       str=input.next();
       }
  }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值