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();
}
}
采用java swing技术制作成可双击使用的小工具:
写一个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);
}
});
}
} ///:~