Guest User

Untitled

a guest
May 20th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. import java.text.ParseException;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4.  
  5. public class HelloWorld {
  6.  
  7. public static void main(String[] args) throws ParseException {
  8.  
  9. // 使用format()方法将日期转换为指定格式的文本
  10. SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
  11. SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd HH:mm");
  12. SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  13.  
  14. // 创建Date对象,表示当前时间
  15. Date now = new Date();
  16. // 调用format()方法,将日期转换为字符串并输出
  17. System.out.println( sdf1.format(now));
  18. System.out.println(sdf2.format(now));
  19. System.out.println(sdf3.format(now));
  20.  
  21. // 使用parse()方法将文本转换为日期
  22. String d = "2014-6-1 21:05:36";
  23. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  24.  
  25. // 调用parse()方法,将字符串转换为日期
  26. Date date = sdf.parse(d);
  27.  
  28. System.out.println(date);
  29. }
  30. }
Add Comment
Please, Sign In to add comment