• 时间戳和字符串之间的互相转换


    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;
    
    }
    }
  • 相关阅读:
    [TJOI2007]小朋友
    弦图小结
    Bzoj2141: 排队
    [SYZOI Round1] 滑稽♂树
    Bzoj2244: [SDOI2011]拦截导弹
    Bzoj1492: [NOI2007]货币兑换Cash(不单调的斜率优化)
    Bzoj1495: [NOI2006]网络收费
    Bzoj1496: [NOI2006]千年虫
    Bzoj1498&1416: [NOI2006]神奇的口袋
    Bzoj4553: [Tjoi2016&Heoi2016]序列
  • 原文地址:https://www.cnblogs.com/androidsuperman/p/3490876.html
Copyright © 2020-2023  润新知