java时间变动的代码 java 时间变量( 二 )


start.addActionListener(this);
stop.addActionListener(this);
reset.addActionListener(this);
mm=new JLabel("00");
minute=new JLabel("00");
second=new JLabel("00");
mm.setFont(font);
minute.setFont(font);
second.setFont(font);
JLabel jl=new JLabel(":");
JLabel jLabel=new JLabel(":");
jl.setFont(font);
jLabel.setFont(font);
setLayout(new GridLayout(2,1));
JPanel jPanel=new JPanel();
JPanel jPanel2=new JPanel();
jPanel.add(minute);
jPanel.add(jl);
jPanel.add(second);
jPanel.add(jLabel);
jPanel.add(mm);
jPanel2.add(start);
jPanel2.add(stop);
jPanel2.add(reset);
add(jPanel);
add(jPanel2);
setTitle("StopWatch");
setLocation(500, 200);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String []args){
new T();
}
@Override
public void actionPerformed(ActionEvent e) {
String s=e.getActionCommand();
if("Start".equals(s)){
timer.start();
start.setEnabled(false);
stop.setEnabled(true);
reset.setEnabled(true);
}else if("Stop".equals(s)){
timer.stop();
start.setEnabled(true);
stop.setEnabled(false);
reset.setEnabled(true);
}else if("Reset".equals(s)){
ms=0;
ss=0;
m=0;
mm.setText("00");
second.setText("00");
minute.setText("00");
timer.stop();
start.setEnabled(true);
stop.setEnabled(true);
}else {
ms++;
if(ms==100){
ms=0;
ss++;
}
if(ss==60){
ss=0;
m++;
}
String s1=ms+"";
String s2=ss+"";
String s3=m+"";
if(s1.length()==1)
s1="0"+s1;
if(s2.length()==1)
s2="0"+s2;
if(s3.length()==1)
s3="0"+s3;
mm.setText(s1);
second.setText(s2);
minute.setText(s3);
}
}
}
编写一个java程序用以将AM/PM格式的时间转换为24小时格式,求大神完成代码SimpleDateFormat objSDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strCurrentTime = objSDateFormat.format(Date类型的时间);
注:大写的HH为24小时制,小写的hh为12小时制 , 当然还可以在ss的后面加上 a,这样可以在后面显示上下文:显示效果为“2008-03-24 17:00:14 下午”
这个更全
实现思路就是输入一个时间 , 之后会输出相应的12小时和24小时效果展示:
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.Scanner;
public class App {
public static void main(String[] args) {
while (true) {
System.out.println("Enter time in 24-hour notation:");
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
try {
outTime(line);
} catch (TimeFormatException e) {
System.out.println("There is no such time as " + line);
System.out.println("Try again:");
continue;
}
【java时间变动的代码 java 时间变量】sc = new Scanner(System.in);
line = sc.nextLine();
if ("n".equalsIgnoreCase(line)) {
break;
}
}
System.out.println("End of program");
}
public static void outTime(String line) throws TimeFormatException {
SimpleDateFormat _24time = new SimpleDateFormat("HH:mm");
SimpleDateFormat _12time = new SimpleDateFormat("hh:mm a",
Locale.ENGLISH);
try {
String[] array = line.split(":");
if (Integer.parseInt(array[0])0
|| Integer.parseInt(array[0])23) {
throw new TimeFormatException();
}
if (Integer.parseInt(array[1])0
|| Integer.parseInt(array[1])59) {
throw new TimeFormatException();
}
System.out.println(_12time.format(_24time.parse(line)));

推荐阅读