使用jYearChooser和jMonthChooser

时间:2016-03-16 05:31:44

标签: java swing simpledateformat jdatechooser

我在java swing工作,我需要从jMonthChooser获得一年的jYearChooser和月份,然后将其格式化为此格式&#34; yyyy-MM-dd 00:00:00 < / EM>&#34; 这是我的代码,但它没有给出预期的输出

Levi Strauss slim fit jeans
Big shopping bag in pink and gold

请帮帮我

1 个答案:

答案 0 :(得分:2)

那是因为参数在Date类构造函数中是特定的:

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

你也可以注意到这个构造函数是@Deprecated。所以更好的想法是使用Calendar

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