方法一:(随机搜索到的)
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class CalenderTest0 {
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
Calendar calendar = Calendar.getInstance();
Date mydate = new Date();
try {
mydate = sdf.parse("201001");
} catch (ParseException e) {
e.printStackTrace();
}
calendar.setTime(mydate);
System.out.println(sdf.format(mydate));
System.out.println(Calendar.MONDAY+"|"+(calendar.get(Calendar.MONDAY)-1));
calendar.set(Calendar.MONDAY,calendar.get(Calendar.MONDAY)-1);
System.out.println(Calendar.DAY_OF_MONTH+"|"+(calendar.get(Calendar.DAY_OF_MONTH)+1));
calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+1);
System.out.println(sdf.format(calendar.getTime()));
}
}
方法二:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class CalenderTest1 {
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
Calendar calendar = Calendar.getInstance();
Date mydate = new Date();
try {
mydate = sdf.parse("201003");
} catch (ParseException e) {
e.printStackTrace();
}
calendar.setTime(mydate);
calendar.add(Calendar.MONTH, -1);
System.out.println(sdf.format(calendar.getTime()));
}
}
本文提供了两个使用Java进行日期操作的方法示例。方法一通过设置月份和天数来改变日期,并展示了如何利用Calendar类进行日期的调整。方法二则演示了如何通过Calendar的add方法来实现月份的递减。
1154

被折叠的 条评论
为什么被折叠?



