java 字符串<=======>时间戳 相互转换
2011-07-27 12:00
253 查看
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class TestTime { public static void main(String[] args) { String time = "2010年12月08日11时17分00秒"; System.out.println(time); // 字符串=======>时间戳 String re_str = getTime(time); System.out.println(re_str); // 时间戳======>字符串 String data = getStrTime(re_str); System.out.println(data); } // 将字符串转为时间戳 public static String getTime(String user_time) { String re_time = null; SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒"); 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; } // 将时间戳转为字符串 public static String getStrTime(String cc_time) { String re_StrTime = null; SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒"); // 例如:cc_time=1291778220 long lcc_time = Long.valueOf(cc_time); re_StrTime = sdf.format(new Date(lcc_time * 1000L)); return re_StrTime; } }
相关文章推荐
- JAVA使用jodatime进行date类型与string类型的时间相互转换
- Java时间格式的相互转换
- 时间戳与字符串相互转换(JAVA)
- java日期与时间戳相互转换大全
- java SimpleDateFormat日期与时间戳的相互转换
- Java、MySql中时间类型与字符串类型的相互转换
- Java时间和时间戳的相互转换
- java 时间戳与日期字符串相互转换
- java 时间戳与日期字符串相互转换
- Java与Unix时间戳的相互转换
- Java与Unix时间戳的相互转换详解
- java里时间戳(Timestamp) String 相互转换
- java字符串和时间类型的相互转换
- java 时间戳与日期字符串相互转换
- java时间戳与日期相互转换
- 浅析Date与Java 时间戳相互转换
- Java中日期时间格式与毫秒数的相互转换
- Java日期时间,以及相互转换
- Java时间和时间戳的相互转换
- java本地时间与UTC时间相互转换