java--根據時間戳得到具體的時間
阿新 • • 發佈:2019-01-31
採用java swing技術製作成可雙擊使用的小工具:public static void main(String[] args) { try { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); // 根據時間戳獲取時間 long nowTime = 1379313866505l; String date = sdf.format(new Date(nowTime)); System.out.println(System.currentTimeMillis()); // 根據時間獲取時間戳 String dateString = "2013-09-23 08:50:11"; Date date1 = sdf.parse(dateString); System.out.println(date1.getTime()); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
寫一個cmd指令碼,雙擊即可開啟工具。
javac TextArea.java pause java TextArea pause 原始碼: package com.swing; //: gui/TextArea.java // Using the JTextArea control. import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; public class TextArea extends JFrame { private JButton b = new JButton("TimeStamp To Date"), d = new JButton("Date To TimeStamp"), c = new JButton("Clear Data"); private JTextArea t = new JTextArea(20, 40); private Map<String,String> m = new HashMap<String,String>(); public TextArea() { // Use up all the data: // m.putAll(Countries.capitals()); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); long nowTime = Long.parseLong(t.getText()); String date = sdf.format(new Date(nowTime)); System.out.println(date); t.append("\n"+date+"\n"); } }); d.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateString = t.getText(); Date date1 = sdf.parse(dateString); System.out.println(date1.getTime()); t.append("\n"+date1.getTime()+"\n"); } catch (Exception e1) { e1.printStackTrace(); } } }); c.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { t.setText(""); } }); setLayout(new FlowLayout()); add(new JScrollPane(t)); add(b); add(d); add(c); } public static void main(String[] args) { run(new TextArea(), 500, 600); } public static void run(final JFrame f, final int width, final int height) { SwingUtilities.invokeLater(new Runnable() { public void run() { f.setTitle(f.getClass().getSimpleName()); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(width, height); f.setVisible(true); } }); } } ///:~