廣告

2015年3月13日 星期五

[ java ] Convert Milliseconds 2 date & Convert date 2 Milliseconds

package test_fb_api; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.TimeZone;
public class convertMilliseconds2date {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  long a= (long) 1.425968092E12;
  long b= (long) 1.4260118825128E12;
  Date date=new Date(b);
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd'T'hh:mm:ss'Z'");
  sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
  String formatTime = sdf.format(date);
  
  System.out.println("convert millisecond to specific date format");
  System.out.println(formatTime);
  System.out.println("");
  
  Date d =new Date();
  try {
    d = sdf.parse("2015/3/27T4:00:54Z");
  } catch (ParseException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  long millisec = d.getTime();
  System.out.println("convert specific date format to millisecond ");
  System.out.println(millisec);
  //after convert date to millisecond.
  System.out.println("");
  //convert your result to date
  //insure your method is correct.
  date=new Date(millisec);
  String formatedTime = sdf.format(date);
  System.out.println(formatedTime);
 }

}

console
convert millisecond to specific date format
2015/03/10T06:24:42Z

convert specific date format to millisecond 
1427428854000

2015/03/27T04:00:54Z

沒有留言:

張貼留言