- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
对于我的第一个 Java 应用程序,我正在制作一个秒表。秒表以小时、分钟和秒的形式显示过去的时间。像这样 00:00:00。
我希望在一定时间(用户定义)过去后显示一个警报(警报框等)。
下面是生成秒表显示的代码(忽略分秒,我们不会为闹钟担心):
swChronometer = new Timer(10, new ActionListener() {
public void actionPerformed(ActionEvent e) {
int decisec = (int)(System.currentTimeMillis() - swWatchStart) / 10; // 10 x millisecond
int seconds = decisec / 100;
int days = seconds / 86400; // 86400 seconds in a day
int hours = (seconds / 3600) - (days * 24); // 3600 seconds in a hour
int min = (seconds / 60) - (days * 1440) - (hours * 60); // 60 seconds in a min
int sec = seconds % 60;
int decis = decisec % 100;
//------------------------------------------------------- format the ints
DecimalFormat formatter = new DecimalFormat("00");
String hoursFormatted = formatter.format(hours);
String minFormatted = formatter.format(min);
String secFormatted = formatter.format(sec);
String decisFormatted = formatter.format(decis);
//------------------------------------------------------- update display
String s = new String(""+hoursFormatted+":"+minFormatted+":"+secFormatted+"");
String sDs = new String(decisFormatted);
swDisplayTimeLabel.setText(s);
swDisplayDeciseconds.setText(sDs);
}
});
如您所见,Timer 整数最终会转换为字符串,然后显示。
After 和 Every 有单选按钮 - 所以在 n 时间后或每 n 时间后发出警报。
我有一个 JSpinner,因此用户可以定义 n 时间,其格式设置为仅显示 00:00:00:
// ----------------------------------
// set spinner format
// ----------------------------------
// inital date at 12pm 00:00:00
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 24); // 24 == 12 PM == 00:00:00
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
swSpinnerTime.setValue(calendar.getTime());
// format field
JSpinner.DateEditor editor = new JSpinner.DateEditor(swSpinnerTime, "HH : mm : ss");
DateFormatter formatter = (DateFormatter)editor.getTextField().getFormatter();
formatter.setAllowsInvalid(false); // sets it so user cant edit colon
formatter.setOverwriteMode(true);
swSpinnerTime.setEditor(editor);
还有一个组合框,因此用户可以选择警报的类型。
每当用户对这些输入中的任何一个进行操作时,都会调用我的函数来处理它:
private void swAlarmSettings() {
//------------------------------------------------------- which radio is selected
String swRadioText = "";
if(swRadioAfter.isSelected()) {
swRadioText = swRadioAfter.getText();
}
if(swRadioEvery.isSelected()) {
swRadioText = swRadioEvery.getText();
}
// debug
System.out.println("Radio Selected: " +swRadioText);
//------------------------------------------------------- which time is selected
Date swSpinnerTimeValue;
swSpinnerTimeValue = (Date)swSpinnerTime.getValue();
// debug
System.out.println("swSpinnerTimeValue: " +swSpinnerTimeValue);
//------------------------------------------------------- which alarm is selected
String swDropDownSelected = (String)swDropDown.getSelectedItem();
// debug
System.out.println("swDropDownSelected: " +swDropDownSelected);
}
然而,无论何时调用此函数,它都会打印出日期:
swSpinnerTimeValue: Tue Oct 14 00:00:00 BST 2014
所以这就是我坚持如何进行的地方。当我已经在 JSpinner 中格式化它时,我没想到会显示整个日期(即使它是错误的(今天是 10 月 13 日,而不是 14 日))。
所以我假设我需要以某种方式解析这个值,去掉我不需要的数据,所以我只剩下 HH:mm:ss 然后将它转换为字符串,这样我就可以比较它到我的秒表显示 (String swDisplayTimeLabel),然后在它们匹配时触发警报。
我希望我已经清楚地表达了我的疑问并提供了足够的信息。感谢您的帮助。
我在 Netbeans 中工作并使用 Swing 设计部分来创建我的应用程序。
最佳答案
Java Date
是一个瞬间,不会保留您预期的输出格式。为此,您需要使用 DateFormat
。例如,
System.out.println("swSpinnerTimeValue: " + new SimpleDateFormat("HH:mm:ss")
.format(swSpinnerTimeValue));
关于Java 如何获得 HH :mm:ss from Date,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26341679/
我正在尝试使用这两种格式解析日期 2014-12-03T10:05:59.5646+08:00: yyyy-MM-dd'T'HH:mm:ss yyyy-MM-dd'T'HH:mm:ssXXX 当我使用
Paypal 返回以下格式的时间戳: yyyy-MM-ddTHH:mm:ssZ 我不知道该怎么办... 如何在 php 中使用本地时区将其转换为 yyyy-MM-dd HH:mm:ss? 我很想pre
我正在使用 Excel 2010 或 Excel 2007 导入包含日期/时间信息的 CSV 文件。我的 CSV 文件中的时间戳具有以下格式:yyyy-mm-dd hh:mm:ss。 (例如:2015
这个问题已经有答案了: Separate Date and Time objects (2 个回答) 已关闭 4 年前。 如何从 SimpleDateFormat("MM/dd/yyyy kk:mm"
这个问题已经有答案了: Java string to date conversion (17 个回答) 已关闭 6 年前。 我需要将日期字符串转换为另一种特定格式。 例如:我有一个日期,可以是 YYY
我想将字符串:24/11/2016 04:30 pm 转换为日期时间值:11/24/2016 04:30 pm。 我的代码为: DateTime date = DateTime.ParseExact(
我想使用 linux 将像“26/11/05 06:00:01,057000000”这样的纪元转换为 yyyy-mm-ddThh:mm:ss? 我曾尝试使用以下脚本但没有成功: echo 26/11/
这个问题在这里已经有了答案: mysql YYYY-MM-DDThh:mm:ss (1 个回答) 关闭 6 年前。 我想上传包含 yyyy-mm-ddThh:mm:ss.sssZ 数据的 csv 文
我在“dd-MM-yyyy HH:mm”中有一个字符串,需要将其转换为格式为日期的对象“yyyy-MM-dd HH:mm”。 下面是我用来转换的代码 oldScheduledDate = "16-05
我有一个数据框(df),它有一个日期列(列名:sale_date),它以以下格式存储数据 dd/mm/yy hh:mm:ss 我正在尝试将其转换为 yyyy-mm-dd hh:mm:ss。尝试了以下但
我的数据库中有日期时间列(格式为 YYYY-mm-dd hh:mm:ss)。 我需要将其转换为 dd-mm-YYYY hh:mm:ss 格式。 我该怎么办?帮助我。 最佳答案 不确定如何在 javas
想知道它们是否代表不同的格式或本质上相同(只是新与旧的演示文稿)。 最佳答案 基于DateTimeFormatter : Offset X and x: This formats the offset
如标题所示,我有一个问题。我需要将 LocalDataTime yyyy-MM-ssThh-mm-ss 解析为 LocalDataTime yyyy-MM-ss hh-mm-ss 但是当我这样做时 S
我想用 mySQL 将我的数据从“yyyy-mm-ddThh-mm-ss.sssZ”转换为“yyyy-mm-dd hh-mm-ss”。 我尝试使用 convert_tz: mysql> SELECT
这个问题在这里已经有了答案: LOAD DATA INFILE easily convert YYYYMMDD to YYYY-MM-DD? (1 个回答) 关闭 6 年前。 我正在尝试将 CSV
我需要更改 string 的日期格式。原始字符串的格式如下: var timeStamp = '2014/07/30 - 14:15:36' 这是我想要实现的日期格式: var timeStampAr
我正在尝试将 yyyy-MM-dd'T'HH:mm:ss.SSSz 格式的日期格式化为 yyyy-mm-dd HH:mm:ss,这应该很容易,但我无法获得它可以工作。 需要解析的日期格式为:2012-
我正在尝试通过传递时间戳作为命令行参数来使用 Synapse 管道运行 Spark 作业。在与 Spark 作业相关的代码运行之前,synapse 正在将字符串命令行参数值从 ISO 格式 2019-
我试过下面的代码: String created_Date = "25-Nov-15 14:23:34"; SimpleDateFormat sdf = new SimpleDateFormat("d
我创建了一个函数,它以与原始格式不同的格式返回日期。基本上,我正在使用此 Select MonthSub('2014-04-10',2)# 语句进行测试,它应该返回2014-02,而不是 2014-0
我是一名优秀的程序员,十分优秀!