如何在Java中从月份和年份生成日期?

问题描述:

我需要为信用卡到期日生成一个新的Date对象,我只有一个月和一年,我怎样才能根据这两个生成一个Date?我需要最简单的方法。我在这里正在阅读其他一些答案,但它们看起来都太复杂了。

I need to generate a new Date object for credit card expiration date, I only have a month and a year, how can I generate a Date based on those two? I need the easiest way possible. I was reading some other answers on here, but they all seem too sophisticated.

您可以使用 java.util.Calendar

Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.YEAR, year);
Date date = calendar.getTime();