java 如何获取当前月份,上个月和两个月前

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32842169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:52:17  来源:igfitidea点击:

How to get current month, previous month and two months ago

java

提问by Daniel Stoica

I need a function that will return three strings:

我需要一个返回三个字符串的函数:

  1. first string will contain the current month and the current year.
  2. second string will contain the previous month and current year.
  3. third string will contain two months ago and current year.
  1. 第一个字符串将包含当前月份和当前年份。
  2. 第二个字符串将包含上个月和当前年份。
  3. 第三个字符串将包含两个月前和当前年份。

This, of course, should also work if current month is January, for example.

当然,如果当前月份是一月,这也应该有效,例如。

So right now, the results should be:

所以现在,结果应该是:

  • September 2015
  • August 2015
  • July 2015
  • 2015 年 9 月
  • 2015 年 8 月
  • 2015 年 7 月

回答by Phylogenesis

A Java 8 version (using the java.time.YearMonthclass) is here.

Java 8 版本(使用java.time.YearMonth类)在这里

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));

This prints the following:

这将打印以下内容:

Today: September 2015

Last Month: August 2015

Two Months Ago: July 2015

今天:2015 年 9 月

上个月:2015 年 8 月

两个月前:2015 年 7 月

回答by Davide Lorenzo MARINO

Calendar c = new GregorianCalendar();
c.setTime(new Date());
SimpleDateFormat sdf = new SimpleDateFormat("MMMM YYYY");
System.out.println(sdf.format(c.getTime()));   // NOW
c.add(Calendar.MONTH, -1);
System.out.println(sdf.format(c.getTime()));   // One month ago
c.add(Calendar.MONTH, -1);
System.out.println(sdf.format(c.getTime()));   // Two month ago

回答by CloudyMarble

  1. Get the Months you need (Current, Current -1 and Current -2) see here
  2. Get the Textual form of the Months like shown here
  1. 获取您需要的月份(当前、当前 -1 和当前 -2)请参见 此处
  2. 获取此处显示的月份的文本形式

回答by Pankaj Saboo

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

}

}