[英]How to get the current system month by 'java.sql.date'?
我想从系统中以字符串形式以及使用java.sql.Date
对象获取当前月份。
我已经尝试过下面的代码,但是Eclipse会删除getMonth()
:
java.util.Date date = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date( date.getMonth() );
为什么是删除线?
因为不赞成使用方法getMonth(),所以该方法在Eclipse中是删除线。 此外,如果您希望将其作为字符串,则可以使用类SimpleDateFormat
来将日期转换为字符串。
例如:
new SimpleDateFormat("MMMM").format(new Date()); // will output 'july'
日期对象的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);
}
}
尝试:
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.