【发布时间】:2016-02-09 10:33:32
【问题描述】:
我正在尝试使用 if 语句根据当前时间向用户显示问候消息。我已经设置了当前时间,但是无法将其用于 if 语句。
public class MyFrame extends JFrame {
public MyFrame() {
super("Greeting");
setbounds(200, 200, 200, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("hh");
JTextArea tx = new JTextArea(10,5);
String myTime = sdf.format(cal.getTime());
if (myTime < 12am && myTime > 12pm)
tx.append("Good Morning");
}
if (myTime <12am && myTime > 12pm)
tx.append("Good Afternoon");
}
JPanel pane = new JPanel();
pane.add(tx);
add(pane);
setVisible(true);
}
public static void main(String [] args) {
new MyFrame();
}
}
【问题讨论】:
-
您收到的错误信息是什么?
-
12am和12pm不是有效的 java 标识符。 -
myTime < 12am && myTime > 12pm?? -
这么多语法错误我都看不懂
-
您希望表达式
12am的类型是什么?
标签: java if-statement simpledateformat