Java日期时间 – Java本地日期时间 本地日期 LocalDate 类表示没有时间或时区的日期。 当时间和时区相关时使用LocalDate。 LocalDate 类包含两个常量,MAX和MIN。 MAX和MIN分别是最大和最小支持的LocalDate。 LocalDate.MAX为+ 999999999-12-31,LocalDate.MIN为-999999999-01-01。 以下代码显示如何创建 LocalDate 对象: import java.time.LocalDate; import java.time.Month; public class Main { public static void main(String[] args) { // Get the current local date LocalDate localDate1 = LocalDate.now(); System.out.println(localDate1); // Create a local date LocalDate localDate2 = LocalDate.of(2014, Month.JUNE, 21); System.out.println(localDate2); // 10000 days after the epoch date 1970-01-01 LocalDate localDate3 = LocalDate.ofEpochDay(10000); System.out.println(localDate3); } } 上面的代码生成以下结果。 例子 以下代码显示如何合并Year和MonthDay以获取LocalDate。 它在未来五年创造圣诞节。 以下代码将为12月25日创建一个MonthDay,并将其合并到一个年份以获取LocalDate。 import java.time.LocalDate; import java.time.Month; import java.time.MonthDay; import java.time.Year; import java.time.format.TextStyle; import java.util.Locale; public class Main { public static void main(String[] args) { MonthDay dec25 = MonthDay.of(Month.DECEMBER, 25); Year year = Year.now(); for (int i = 1; i <= 5; i++) { LocalDate ld = year.plusYears(i).atMonthDay(dec25); int yr =...
Java日期时间 – Java年月日 年 年表示一年,例如2012年,2013年等。 以下代码显示如何创建Year对象并对其执行基本操作。 import java.time.Year; public class Main { public static void main(String[] args) { Year y1 = Year.of(2014); System.out.println(y1); Year y2 = y1.minusYears(1); System.out.println(y2); Year y3 = y1.plusYears(1); System.out.println(y3); Year y4 = Year.now(); System.out.println(y4); if (y1.isLeap()) { System.out.println(y1 + " is a leap year."); } else { System.out.println(y1 + " is not a leap year."); } } } 上面的代码生成以下结果。 年月 年月表示年和月的有效组合,例如2012-05,2013-09等。 以下代码显示了如何创建Year Month对象并对其执行一些基本操作。 import java.time.Month; import java.time.YearMonth; public class Main { public static void main(String[] args) { YearMonth ym1 = YearMonth.of(2014, Month.JUNE); int monthLen = ym1.lengthOfMonth(); System.out.println(monthLen); int yearLen = ym1.lengthOfYear(); System.out.println(yearLen); } } 上面的代码生成以下结果。 月 Month 枚举有12个常量来表示12个月。 常量名称为 一月,二月,三月,四月,五月,六月,七月,八月,九月,十月,十一月和十二月。 Month 枚举从1到12按顺序编号,其中一月为1,十二月为12。 Month枚举从int值创建一个Month实例。 我们可以使用from()从date对象创建Month。 要获取Month的int值,请使用Month枚举 getValue()方法。 import java.time.LocalDate; import java.time.Month; public class Main { public static...
JavaFX教程 – JavaFX 进度显示器 进度指示器以动态更改饼图的形式显示JavaFX中的操作进度。 以下代码显示如何使用不确定值创建ProgressIndicator。 import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.ProgressIndicator; import javafx.stage.Stage; public class Main extends Application { @Override public void start(Stage stage) { Group root = new Group(); Scene scene = new Scene(root, 260, 80); stage.setScene(scene); Group g = new Group(); ProgressIndicator p1 = new ProgressIndicator(); g.getChildren().add(p1); scene.setRoot(g); stage.show(); } public static void main(String[] args) { launch(args); } } 上面的代码生成以下结果。 创建进度条显示器 以下代码通过传递progress值来创建ProgressIndicator。 ProgressIndicator pi = new ProgressIndicator(0.6); 我们可以使用空构造函数创建没有参数的进度指示器。然后我们可以使用setProgress方法分配值。 如果无法确定进度,我们可以在不确定模式下设置进度控制,直到确定任务的长度。 以下代码显示了如何创建一个ProgressIndicator,完成25%。 import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.ProgressIndicator; import javafx.stage.Stage; public class Main extends Application { @Override public void start(Stage stage) { Group root = new Group(); Scene scene = new Scene(root, 260, 80); stage.setScene(scene); Group g = new Group(); ProgressIndicator p1 = new ProgressIndicator();...
JavaFX教程 – JavaFX进度条 进度条可视化JavaFX应用程序中的操作进度。 import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.ProgressBar; import javafx.stage.Stage; public class Main extends Application { @Override public void start(Stage stage) { Group root = new Group(); Scene scene = new Scene(root, 260, 80); stage.setScene(scene); Group g = new Group(); ProgressBar p2 = new ProgressBar(); g.getChildren().add(p2); scene.setRoot(g); stage.show(); } public static void main(String[] args) { launch(args); } } 上面的代码生成以下结果。 创建ProgressBar 以下代码显示如何通过传递progress值来创建ProgressBar。 ProgressBar pb = new ProgressBar(0.6); 您还可以使用空构造函数创建没有参数的进度条。然后使用setProgress方法分配值。 如果我们不能确定任务的完全完成时间,我们可以设置进度条在不确定模式,直到确定任务的长度。 以下代码显示如何创建一个25%完成的ProgressBar。 import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.ProgressBar; import javafx.stage.Stage; public class Main extends Application { @Override public void start(Stage stage) { Group root = new Group(); Scene scene = new Scene(root, 260, 80); stage.setScene(scene); Group g = new Group(); ProgressBar p2 = new ProgressBar(); p2.setProgress(0.25F); g.getChildren().add(p2);...