Advertisement
Nekkox

Convert a Date String to Unix Timestamp

Mar 27th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.56 KB | None | 0 0
  1. /**
  2.  *
  3.  * @param strDate: "yyyy-MM-dd hh:mm:ss"
  4.  * @return unixtime, it´s the unix timestamp in long integer.
  5.  *                0, in case of error.
  6.  */
  7. private long convertStringTotimestamp(String strDate){
  8.     try{
  9.     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss z");
  10.         Date parsedDate = dateFormat.parse(strDate);
  11.         long unixTime = (long) parsedDate.getTime()/1000;
  12.         return unixTime;
  13.     }catch(Exception e){
  14.         Print.logError((String)"Cannot convert Unit's UTC time to timestamp.");
  15.         return 0;
  16.     }
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement