package com.heima.homework;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
import com.sun.xml.internal.ws.api.pipe.NextAction;
public class Test3 {
/**
* (1)通过键盘录入日期字符串,格式(2015-10-20)
(2)定义方法获取该日期的前一天
(3)定义方法获取该日期的上个月份
(4)定义方法获取该日期的所在月份的第一天
(5)定义方法获取该日期的所在月份的最后一天
* @throws ParseException
*/
public static void main(String[] args) throws ParseException {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
SimpleDateFormat sdt = new SimpleDateFormat("yyyy-MM-dd");
Date d = sdt.parse(s);
Calendar c = Calendar.getInstance();
c.setTime(d);
//demo1(c);
//demo2(c);
//demo3(c);
c.add(Calendar.MONTH, 1);
c.set(Calendar.DAY_OF_MONTH, 1);
c.add(Calendar.DAY_OF_MONTH, -1);
System.out.println(c.get(Calendar.YEAR)+"-"+ ((c.get(Calendar.MONTH)+1))
+"-"+ c.get(Calendar.DAY_OF_MONTH));
}
private static void demo3(Calendar c) {
c.set(Calendar.DAY_OF_MONTH, 1);
System.out.println(c.get(Calendar.YEAR)+"-"+ ((c.get(Calendar.MONTH)+1))
+"-"+ c.get(Calendar.DAY_OF_MONTH));
}
private static void demo2(Calendar c) {
c.add(Calendar.MONTH, -1);
System.out.println(c.get(Calendar.YEAR)+"-"+ ((c.get(Calendar.MONTH)+1))
+"-"+ c.get(Calendar.DAY_OF_MONTH));
}
private static void demo1(Calendar c) {
c.add(Calendar.DAY_OF_MONTH, -1);
System.out.println(c.get(Calendar.YEAR)+"-"+ ((c.get(Calendar.MONTH)+1))
+"-"+ c.get(Calendar.DAY_OF_MONTH));
}
}
|
|