fork(2) download
  1. class Main {
  2. public static void main (String[] args) throws Exception {
  3. Main m = new Main();
  4. m.test1();
  5. System.out.println();
  6. m.test2();
  7. }
  8.  
  9. void test1() throws Exception {
  10. System.out.println("TEST 1: ");
  11.  
  12. String strTimestamp = "1957-04-27 00:00:00.01";
  13. System.out.println(strTimestamp + " [Original String]");
  14.  
  15. String format = "yyyy-MM-dd HH:mm:ss.SS";
  16. System.out.println(format + " [Format used]");
  17. java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(format);
  18.  
  19. // Convert String to Date:
  20. java.util.Date date = formatter.parse(strTimestamp);
  21. long time = date.getTime();
  22. System.out.println(formatter.format(time) + " [Date#getTime() with same format]");
  23.  
  24. java.sql.Timestamp timestamp = new java.sql.Timestamp(time);
  25. System.out.println(timestamp + " [Timestamp]");
  26. }
  27.  
  28. void test2() throws Exception {
  29. System.out.println("TEST 2: ");
  30.  
  31. String strTimestamp = "1957-04-27 00:00:00.001";
  32. System.out.println(strTimestamp + " [Original String]");
  33.  
  34. String format = "yyyy-MM-dd HH:mm:ss.SSS";
  35. System.out.println(format + " [Format used]");
  36. java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(format);
  37.  
  38. // Convert String to Date:
  39. java.util.Date date = formatter.parse(strTimestamp);
  40. long time = date.getTime();
  41. System.out.println(formatter.format(time) + " [Date#getTime() with same format]");
  42.  
  43. java.sql.Timestamp timestamp = new java.sql.Timestamp(time);
  44. System.out.println(timestamp + " [Timestamp]");
  45. }
  46. }
Success #stdin #stdout 0.06s 711680KB
stdin
Standard input is empty
stdout
TEST 1: 
1957-04-27 00:00:00.01 [Original String]
yyyy-MM-dd HH:mm:ss.SS [Format used]
1957-04-27 00:00:00.01 [Date#getTime() with same format]
1957-04-27 00:00:00.001 [Timestamp]

TEST 2: 
1957-04-27 00:00:00.001 [Original String]
yyyy-MM-dd HH:mm:ss.SSS [Format used]
1957-04-27 00:00:00.001 [Date#getTime() with same format]
1957-04-27 00:00:00.001 [Timestamp]