1

我想从系统中获取当前月份作为字符串并使用java.sql.Date对象。

我已经尝试了下面的代码,但是getMonth()Eclipse删除了:

java.util.Date date = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date( date.getMonth() );

为什么会被击穿?

4

3 回答 3

9

该方法在 Eclipse 中被删除,因为方法 getMonth() 已被弃用。此外,如果您希望它作为字符串,您可以使用该类SimpleDateFormat将日期转换为字符串。

例如:

new SimpleDateFormat("MMMM").format(new Date()); // will output 'july'
于 2012-07-19T14:34:34.697 回答
2

日期对象的 getMonth() 函数已弃用:

getMonth() 
    Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.MONTH)

以下是使用日历类的代码

    import java.util.Calendar;

    public class GetMonth
    {
      public static void main(String[] args)
      {
      String[] months = {"January", "February",
      "March", "April", "May", "June", "July",
      "August", "September", "October", "November",
      "December"};

      Calendar cal = Calendar.getInstance(); 
      String month = months[cal.get(Calendar.MONTH)];
      System.out.println("Month name: " + month);
      }
    }

参考

于 2012-07-19T14:36:16.403 回答
1

尝试:

import java.util.Calendar;

public class GetMonth
{
  public static void main(String[] args)
  {
  String[] months = {"January", "February",
  "March", "April", "May", "June", "July",
  "August", "September", "October", "November",
  "December"};

  Calendar cal = Calendar.getInstance(); 
  String month = months[cal.get(Calendar.MONTH)];
  System.out.println("Month name: " + month);
  }
}

来源

于 2012-07-19T14:34:25.887 回答