Calendar 对象将 3 月打印为月份,将 1 打印为年份
Calendar object keeps printing March as the month and 1 as they year
有谁知道为什么我的日历对象一直打印 3 月作为月份,1 作为年份?有没有办法将日历设置为当前的月份和日期?
import java.util.Date;
import java.util.GregorianCalendar;
import javafx.application.Application;
public class Calendar extends Application{
@Override
public void start(Stage stage) throws Exception {
BorderPane pane = new BorderPane();
// Create a calendar
GregorianCalendar calendar = new GregorianCalendar();
Date time = new Date();
calendar.setTime(time);
// Create title
Text header = new Text(getMonth(calendar.MONTH) + ", " + calendar.YEAR);
// place title in pane
pane.setTop(header);
BorderPane.setAlignment(header, Pos.CENTER);
Scene scene = new Scene(pane);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Text header = new Text(getMonth(calendar.get(Calendar.MONTH)
+ ", " + calendar.get(Calendar.YEAR));
Calendar.get方法使用int常量获取特定的MONTH/YEAR字段。
请注意,我相信那个月是从 0 开始计算的。
Java 8 data/time 类 好多了(虽然一开始有点压倒性)。
编辑以提供更多解释:
Calendar 对象上的 get 和 set 方法将期望字段 number/positions 到 return 的值,并且这些数字被定义为常量,如 MONTH、YEAR、DAY、DAY_OF_MONTH等,
因此,当您使用 calendar.Month
时,它只会 return 您输入字段编号的值,而不是实际的月份。您需要将其与 get / set 方法结合使用,以获取日历实例中的实际值。
示例
Text header = new Text(getMonth(calendar.get(Calendar.MONTH) + ", " + calendar.get(Calendar.YEAR));
java.time
顺便说一句,这种工作在 Java 8 及更高版本中内置的 java.time 框架中要容易得多。这些新的 类 取代了旧的 java.util.Date/.Calendar 类,后者已被证明非常麻烦。
确定今天的日期。
ZoneId zoneId = ZoneId.of ( "America/Montreal" );
LocalDate today = LocalDate.now ( zoneId );
使用特定格式生成该日期对象的字符串表示形式。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "M, yyyy" );
String output = today.format ( formatter );
更好的是,生成本地化格式的字符串表示形式。
DateTimeFormatter formatterLocalized = DateTimeFormatter.ofLocalizedDate ( FormatStyle.FULL ).withLocale ( Locale.CANADA_FRENCH );
String outputLocalized = today.format ( formatterLocalized );
转储到控制台。
System.out.println ( "today: " + today + " is: " + output + " which in localized format is: " + outputLocalized );
当运行.
today: 2015-10-17 is: 10, 2015 which in localized format is: samedi 17 octobre 2015
有谁知道为什么我的日历对象一直打印 3 月作为月份,1 作为年份?有没有办法将日历设置为当前的月份和日期?
import java.util.Date;
import java.util.GregorianCalendar;
import javafx.application.Application;
public class Calendar extends Application{
@Override
public void start(Stage stage) throws Exception {
BorderPane pane = new BorderPane();
// Create a calendar
GregorianCalendar calendar = new GregorianCalendar();
Date time = new Date();
calendar.setTime(time);
// Create title
Text header = new Text(getMonth(calendar.MONTH) + ", " + calendar.YEAR);
// place title in pane
pane.setTop(header);
BorderPane.setAlignment(header, Pos.CENTER);
Scene scene = new Scene(pane);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Text header = new Text(getMonth(calendar.get(Calendar.MONTH)
+ ", " + calendar.get(Calendar.YEAR));
Calendar.get方法使用int常量获取特定的MONTH/YEAR字段。 请注意,我相信那个月是从 0 开始计算的。
Java 8 data/time 类 好多了(虽然一开始有点压倒性)。
编辑以提供更多解释:
Calendar 对象上的 get 和 set 方法将期望字段 number/positions 到 return 的值,并且这些数字被定义为常量,如 MONTH、YEAR、DAY、DAY_OF_MONTH等,
因此,当您使用 calendar.Month
时,它只会 return 您输入字段编号的值,而不是实际的月份。您需要将其与 get / set 方法结合使用,以获取日历实例中的实际值。
示例
Text header = new Text(getMonth(calendar.get(Calendar.MONTH) + ", " + calendar.get(Calendar.YEAR));
java.time
顺便说一句,这种工作在 Java 8 及更高版本中内置的 java.time 框架中要容易得多。这些新的 类 取代了旧的 java.util.Date/.Calendar 类,后者已被证明非常麻烦。
确定今天的日期。
ZoneId zoneId = ZoneId.of ( "America/Montreal" );
LocalDate today = LocalDate.now ( zoneId );
使用特定格式生成该日期对象的字符串表示形式。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "M, yyyy" );
String output = today.format ( formatter );
更好的是,生成本地化格式的字符串表示形式。
DateTimeFormatter formatterLocalized = DateTimeFormatter.ofLocalizedDate ( FormatStyle.FULL ).withLocale ( Locale.CANADA_FRENCH );
String outputLocalized = today.format ( formatterLocalized );
转储到控制台。
System.out.println ( "today: " + today + " is: " + output + " which in localized format is: " + outputLocalized );
当运行.
today: 2015-10-17 is: 10, 2015 which in localized format is: samedi 17 octobre 2015