我编写了以下 java 代码以将日期和时间格式化为特定格式。您可以在ideone看到以下代码。
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.text.SimpleDateFormat;
class timeAndDateTransformation{
public static void main(String[] argv){
Calendar newDate = new GregorianCalendar(2009,7,1,15,20,00);
SimpleDateFormat dateFormant = new SimpleDateFormat("yyyy/MM/dd");
SimpleDateFormat timeFormant = new SimpleDateFormat("HH:mm:ss");
System.out.println(dateFormant.format(newDate.getTime()).toString());
System.out.println(timeFormant.format(newDate.getTime()).toString());
}
}
它给了我以下输出:
2009/08/01
15:20:00
在这个输出中,除了月份之外,一切都很好。我一个月通过了 7,但在这个格式输出中,它给出了 8 作为输出。请指出我在哪里做错了。我对java的日期/日历类不是很熟悉,所以请多多包涵。