我想從系統中獲取當前月份作爲字符串,並使用java.sql.Date
對象。如何通過'java.sql.date'獲取當前系統月份?
我曾嘗試下面的代碼,但getMonth()
是打擊通過Eclipse:
java.util.Date date = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date(date.getMonth());
爲什麼是透?
我想從系統中獲取當前月份作爲字符串,並使用java.sql.Date
對象。如何通過'java.sql.date'獲取當前系統月份?
我曾嘗試下面的代碼,但getMonth()
是打擊通過Eclipse:
java.util.Date date = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date(date.getMonth());
爲什麼是透?
的方法,是因爲該方法得到月()不推薦使用在Eclipse透。 此外,如果您希望將它作爲字符串,則可以使用類SimpleDateFormat
將日期轉換爲字符串。
例如:
new SimpleDateFormat("MMMM").format(new Date()); // will output 'july'
嘗試:
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);
}
}
日期對象的得到月()函數被棄用:
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);
}
}
Simples t答案!謝謝! – Mistu4u 2012-07-20 12:45:12