java的 System.currentTimeMillis 方法获取的值是精确到 毫秒的
但是 我数据库里面的只需要精确到秒就可以了这个可以节省3位的长度 Java 有办法获取到 只精确到秒的时间戳么?毫秒 13位  1278392014312
秒   10位  1278392014是否有直接将 1278392014 (10位的时间戳) 转成时间的方法?Timestamp 好想无法转换 1278392014 时间不对 只能再后面加 000 这样才能转换 但是个人感觉这样太麻烦了  

解决方案 »

  1.   

    你数据库存的是时间类型,塞数据的时候就应该对应Date类型啊
      

  2.   

    自己 乘1000 除1000就可以了,默认不提供这样的支持
    不过我觉得正常情况下都是使用数据库的Date、DateTime(Timestamp)字段类型,不应该自己用int存储
      

  3.   

    这确实是个好方法 但是我更想知道 Java 是否直接带了这样的方法 呵呵~~
      

  4.   

    很简单,就是乘除1000嘛。自己写个工具类。    /**
         * 将时间转换为显示字符串,“全长”格式 2004-08-16 12:08:01
         *
         * @param second 相对于 1970年1月1日零时的秒数
         * @return
         */
        public static String toTimeFullString(long second) {
            StringBuffer sbRet = new StringBuffer();
            try {
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(second * 1000);            sbRet.append(calendar.get(Calendar.YEAR)).append("-");
                sbRet.append(integerToString((calendar.get(Calendar.MONTH) + 1), 2)).append("-");
                sbRet.append(integerToString(calendar.get(Calendar.DATE), 2)).append(" ");
                sbRet.append(integerToString(calendar.get(Calendar.HOUR_OF_DAY), 2)).append(":");
                sbRet.append(integerToString(calendar.get(Calendar.MINUTE), 2)).append(":");
                sbRet.append(integerToString(calendar.get(Calendar.SECOND), 2));
            } catch (Exception e) {/* DISCARD EXEPTION */
            }
            return sbRet.toString();
        }    /**
         * 将时间字符串转化为秒值(相对 1970年1月1日)
         *
         * @param time   时间字符串
         * @param format 格式串,如 "yyyy-MM-dd-HH-mm", "yyyy-MM-dd HH:mm:ss"
         * @return   失败返回 0
         */
        public static int toTimeInteger(String time, String format) {
            int nRet = 0;
            try {
                java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat(format);
                Date de = dateFormat.parse(time);
                nRet = (int) (de.getTime() / 1000);
            } catch (Exception e) {/*DISCARD EXCEPTION*/
            }
            return nRet;
        }
      

  5.   

    Date和Calendar都没有这样的方法或构造器,只能自己动手了。
      

  6.   

    使用Calendar 先设置时间 然后将毫秒set 成0
    别写什么除以一千,有现成的API干什么不用?
      

  7.   

    感谢大家帮忙了 现在知道了 因为想让时间在数据库里面用int存 呵呵~个人比较懒 所以比较喜欢API 自带的方法搞定问题大家帮了很多忙 然后把自己写的方法贡献出来 
    import java.util.Date;
    import java.util.Locale;import org.apache.commons.lang.time.DateFormatUtils;public class TimeStampUtil { public static class TimeFormat{

    private TimeFormat(String pattern, Locale locale){
    this.pattern = pattern;
    this.locale = locale;
    }

    public static TimeFormat getInstance(String pattern, Locale locale){
    return new TimeFormat(pattern, locale);
    }

    public static TimeFormat getInstance(String pattern){
    return new TimeFormat(pattern, null);
    }
    private String pattern;

    private Locale locale;

    public String getPattern() {
    return pattern;
    } public Locale getLocale() {
    return locale;
    }

    }

    public static int getTimeStamp() {
    long longTime = System.currentTimeMillis();
    return (int) (longTime / 1000);
    } public static Date formatTimeStamp(int timestamp) {
    long longTime = toLong(timestamp);
    return new Date(longTime);
    } private static long toLong(int timestamp) {
    return (long) timestamp * 1000;
    }

    public static String formatTimeStamp(int timestamp, String pattern){
    return DateFormatUtils.format(toLong(timestamp), pattern);
    } public static String formatTimeStamp(int timestamp, TimeFormat timeFormat){
    return DateFormatUtils.format(toLong(timestamp), timeFormat.getPattern(), timeFormat.getLocale());
    }

    public static final TimeFormat FULL_CHINA_TIME = TimeFormat.getInstance("yyyy年MM月dd日 HH点mm分ss秒");
    public static final TimeFormat CHINA_TIME = TimeFormat.getInstance("yyyy年MM月dd日 HH:mm:ss");
    public static final TimeFormat DEFAULT_TIME = TimeFormat.getInstance("yyyy-MM-dd HH:mm:dd");
    public static final TimeFormat FULL_ENGLISH_TIME = TimeFormat.getInstance("EEE d MMM yyyy HH:mm:ss z", Locale.US);
    public static final TimeFormat ENGLISH_TIME = TimeFormat.getInstance("EEE d MMM yyyy HH:mm:ss", Locale.US);
    }
      

  8.   

    public static long getTimestamp(String dateStr){
      Date date1 = null;
      Date date2 = null;
    try {
    date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStr);
    } catch (ParseException e) {e.printStackTrace();}

    try {
    date2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("1970-01-01 08:00:00");
    } catch (ParseException e){e.printStackTrace();}
      long l = date1.getTime() - date2.getTime() > 0 ? date1.getTime()- date2.getTime() : 
       date2.getTime() - date1.getTime();
      long rand = (l/1000);
      return  rand;
    }
      

  9.   

    long now = System.currentTimeMillis();
    SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    now = now - now%1000 ;
    String realTime = simple.format(now);
    System.out.println(now+"\t"+realTime);
      

  10.   

    存入数据库时候 :now = now - now%1000 ;
    now = now/1000 ;
    取出来的时候 乘以 1000