Java文字列<->タイムスタンプ



Java String Timestamp



package ceshi import java.text.ParseException import java.text.SimpleDateFormat import java.util.Date public class stringinttimestamp { public static void main(String[] args) { String time = '11:17:00 on December 08, 2010' System.out.println(time) // string =======>timestamp String re_str = getTime(time) System.out.println(re_str) // timestamp ======>string String data = getStrTime('1316626497') System.out.println(data) } / / Turn the string into a timestamp public static String getTime(String user_time) { String re_time = null SimpleDateFormat sdf = new SimpleDateFormat('yyyy year MM month dd day HH mm split ss seconds') Date d try { d = sdf.parse(user_time) long l = d.getTime() String str = String.valueOf(l) re_time = str.substring(0, 10) } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace() } return re_time } / / Turn the timestamp into a string public static String getStrTime(String cc_time) { String re_StrTime = null SimpleDateFormat sdf = new SimpleDateFormat('yyyy year MM month dd day HH mm split ss seconds') // For example: cc_time=1291778220 long lcc_time = Long.valueOf(cc_time) re_StrTime = sdf.format(new Date(lcc_time * 1000L)) return re_StrTime } }