我需要一个返回三个字符串的函数:
当然,如果当前月份是1月,这也应该有效。
现在,结果应该是:
答案 0 :(得分:7)
Java 8版本(使用java.time.YearMonth
类)是here。
YearMonth thisMonth = YearMonth.now();
YearMonth lastMonth = thisMonth.minusMonths(1);
YearMonth twoMonthsAgo = thisMonth.minusMonths(2);
DateTimeFormatter monthYearFormatter = DateTimeFormatter.ofPattern("MMMM yyyy");
System.out.printf("Today: %s\n", thisMonth.format(monthYearFormatter));
System.out.printf("Last Month: %s\n", lastMonth.format(monthYearFormatter));
System.out.printf("Two Months Ago: %s\n", twoMonthsAgo.format(monthYearFormatter));
这将打印以下内容:
今天:2015年9月
上个月:2015年8月
两个月前:2015年7月
答案 1 :(得分:4)
function (localStorageService, backendUpdate) {
答案 2 :(得分:-1)
答案 3 :(得分:-1)
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public static void main(String[] args) {
Date currentDate = null;
String dateString = null;
try {
Calendar c = new GregorianCalendar();
c.set(Calendar.HOUR_OF_DAY, 0); // anything 0 - 23
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
//c.add(Calendar.MONTH, -1);//previous month
//c.add(Calendar.MONTH, -2);//two months back
currentDate = c.getTime(); // the midnight, that's the first second
// of the day.
SimpleDateFormat sdfr = new SimpleDateFormat("MMMM yyyy");
dateString = sdfr.format(currentDate);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(dateString); //prints current date
}