Java 使用jYearChooser和jMonthChooser
我在java swing中工作,我需要从jMonthChooser中获取年份和月份,然后将其格式化为格式“yyy-MM-dd 00:00:00” 这是我的代码,但它没有给出预期的输出Java 使用jYearChooser和jMonthChooser,java,swing,simpledateformat,jdatechooser,Java,Swing,Simpledateformat,Jdatechooser,我在java swing中工作,我需要从jMonthChooser中获取年份和月份,然后将其格式化为格式“yyy-MM-dd 00:00:00” 这是我的代码,但它没有给出预期的输出 int year = jYearChooser1.getYear(); int month = jMonthChooser1.getMonth(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd 00:00:00"); Date dt
int year = jYearChooser1.getYear();
int month = jMonthChooser1.getMonth();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
Date dt = new Date(year, month,00);
Date todate = dateFormat.format(dt);
请帮助我这是因为年参数在日期类构造函数中是特定的:
**
* Allocates a <code>Date</code> object and initializes it so that
* it represents midnight, local time, at the beginning of the day
* specified by the <code>year</code>, <code>month</code>, and
* <code>date</code> arguments.
*
* @param year the year minus 1900.
* @param month the month between 0-11.
* @param date the day of the month between 1-31.
* @see java.util.Calendar
* @deprecated As of JDK version 1.1,
* replaced by <code>Calendar.set(year + 1900, month, date)</code>
* or <code>GregorianCalendar(year + 1900, month, date)</code>.
*/
@Deprecated
public Date(int year, int month, int date) {
this(year, month, date, 0, 0, 0);
}
您还可以注意到这个构造函数是。所以更好的办法是使用:
你到底期望什么?产量是多少?我选择2016年作为年份,3月作为月份,但产量是3916-03-29 00:00:00@AlexanderBaltasar
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
Calendar c = Calendar.getInstance();
c.set(year, month, 1); // Specify day of month
String formattedDate = dateFormat.format(c.getTime());