我需要获取今天的日期和偏移日期的月份和日期。我就是这样做的:
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_WEEK, 0);
Date today = calendar.getTime();
System.out.println(today);
输出:
Wed Aug 27 15:07:35 CEST 2014
两件事,我需要月份和日期为数字,如8/27。我明白如何在今天的日期这样做:
int day = calendar.get(Calendar.DAY_OF_MONTH);
int month = calendar.get(Calendar.MONTH);
String a = String.valueOf(day);
String b = String.valueOf(month);
System.out.println(b +"/" + a);
我的问题是我可能需要在该日期添加一个偏移,如果我想要明天约会的话。有没有办法做到这一点,因为转换8月27日星期三....到8/27只会是一个痛苦。感谢
答案 0 :(得分:2)
使用简单的日期格式:
类似的东西:
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_WEEK, 1);
Date today = calendar.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
System.out.println(sdf.format(today));
答案 1 :(得分:1)
DateFormat formatter = new SimpleDateFormat("MM/dd");
Calendar cal = Calendar.getInstance();
String calAsString = formatter.format(cal.getTime());
System.out.println(calAsString);
// Now for tomorrow's date:
int offset = 1;
cal.add(Calendar.DATE, offset);
calAsString = formatter.format(cal.getTime());
System.out.println(calAsString);
答案 2 :(得分:0)
使用日历为当天添加值:
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH,1)