日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

基于JavaFX的SimpleDateFormat演示程序

發(fā)布時(shí)間:2023/12/3 java 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基于JavaFX的SimpleDateFormat演示程序 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
對(duì)于使用Java Date進(jìn)行格式化的新手甚至對(duì)于使用Java Date進(jìn)行格式化的有經(jīng)驗(yàn)的Java開(kāi)發(fā)人員而言,可能有些棘手的事情是使用SimpleDateFormat規(guī)范日期/時(shí)間格式。 SimpleDateFormat的基于類(lèi)級(jí)別的Javadoc的文檔非常詳盡,涵蓋了表示日期/時(shí)間的各個(gè)組成部分的模式。 但是,除非人們仔細(xì)閱讀并理解了這些不同的模式,否則要記住月份的小寫(xiě)字母“ d”和一年中的大寫(xiě)字母“ D”之間的區(qū)別或者記住是否是小寫(xiě)字母“ m”或“大寫(xiě)字母“ M”使用了幾個(gè)月而不是幾分鐘。 在本文中,我看一個(gè)用JavaFX編寫(xiě)的簡(jiǎn)單應(yīng)用程序,它使開(kāi)發(fā)人員可以快速嘗試任意模式,以查看SimpleDateFormat如何在給定任意模式的情況下呈現(xiàn)當(dāng)前日期/時(shí)間。 從理論上講,開(kāi)發(fā)人員可以使用這個(gè)簡(jiǎn)單的工具快速確定其日期/時(shí)間模式的效果,但這實(shí)際上是應(yīng)用JavaFX的一個(gè)借口。

下面的代碼清單包含完整的基于JavaFX 2.x的應(yīng)用程序。

package dustin.examples;import java.text.SimpleDateFormat; import java.util.Date; import javafx.application.Application; import javafx.event.EventHandler; import javafx.geometry.Pos; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.TextField; import javafx.scene.control.TextFieldBuilder; import javafx.scene.input.MouseEvent; import javafx.scene.layout.Pane; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.stage.Stage;/*** JavaFX application allowing for testing and demonstration of various String* formats for date/time.* * @author Dustin*/ public class DateTimeStringFormatDemonstrator extends Application {/*** Generate the application's main pane.* * @return Main pane for the application.*/private Pane generateMainPane(){final VBox vbox = new VBox();final TextField dateTimeFormatField =TextFieldBuilder.create().prefWidth(350).alignment(Pos.CENTER).promptText("Enter DateFormat").build();vbox.getChildren().add(dateTimeFormatField);final TextField formattedDateField =TextFieldBuilder.create().prefWidth(350).alignment(Pos.BASELINE_CENTER).promptText("Date Output Goes Here").build();formattedDateField.setEditable(false);final Button applyButton = new Button("Apply Format");applyButton.setPrefWidth(350);applyButton.setOnMousePressed(new EventHandler<MouseEvent>(){@Overridepublic void handle(MouseEvent mouseEvent){try{final SimpleDateFormat sdf =new SimpleDateFormat(dateTimeFormatField.getText());formattedDateField.setText(sdf.format(new Date()));formattedDateField.setAlignment(Pos.CENTER);}catch (Exception ex){formattedDateField.setText("ERROR");formattedDateField.setAlignment(Pos.CENTER);}formattedDateField.setAlignment(Pos.BASELINE_CENTER);}});vbox.getChildren().add(applyButton);vbox.getChildren().add(formattedDateField);return vbox;}/*** The method overridden from Application for starting the application.* * @param stage Primary stage.* @throws Exception Exceptions throwing during execution of JavaFX application.*/@Overridepublic void start(final Stage stage) throws Exception{stage.setTitle("JavaFX Date/Time String Format Presenter");final Group group = new Group();group.getChildren().add(generateMainPane());final Scene scene = new Scene(group, 350, 65, Color.DARKKHAKI);stage.setScene(scene);stage.show();}/*** Main function for running date/time format JavaFX application.* * @param arguments Command-line arguments; none expected.*/public static void main(final String[] arguments){Application.launch(arguments);} } 上面顯示的基于JavaFX 2的簡(jiǎn)單應(yīng)用程序使您可以輕松地嘗試不同的日期/時(shí)間格式模式,以查看SimpleDateFormat將如何處理每種模式。 接下來(lái)顯示在2012年5月8日星期二晚上使用的一系列此類(lèi)。 這些示例演示了使用SimpleDateFormat幾個(gè)關(guān)鍵方面:
  • 大寫(xiě)字母“ M”用于幾個(gè)月,小寫(xiě)字母“ m”用于分鐘。
  • “ M”字符的數(shù)量表示月份的表示形式(例如:5月的5、05或“ 5月”)。
  • 大寫(xiě)字母“ D”表示年份中的日期(自1月1日開(kāi)始),小寫(xiě)字母“ d”表示月份中的日期(自5月1日開(kāi)始)。
  • 兩個(gè)'y'或'Y'數(shù)字代表2位數(shù)字的年份,但是3或4個(gè)'Y'或'y'數(shù)字可用于4位數(shù)字的年份。
本博客文章中突出顯示的簡(jiǎn)單示例演示了JavaFX的簡(jiǎn)單性,并提供了JavaFX如何提供圖形界面以使Java應(yīng)用程序更直觀的示例。 作為其一部分,將演示JavaFX中的鼠標(biāo)事件處理以及使用構(gòu)建器的常見(jiàn)JavaFX習(xí)慣用法 。 此應(yīng)用程序的實(shí)際用途是快速輕松地確定由SimpleDateFormat為給定模式提供的表示形式。

參考:來(lái)自JCG合作伙伴 Dustin Marx 的基于JavaFX的SimpleDateFormat演示器,來(lái)自Inspired by Actual Events博客。


翻譯自: https://www.javacodegeeks.com/2012/05/javafx-based-simpledateformat.html

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的基于JavaFX的SimpleDateFormat演示程序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。