java的 System.currentTimeMillis 方法获取的值是精确到 毫秒的
但是 我数据库里面的只需要精确到秒就可以了这个可以节省3位的长度 Java 有办法获取到 只精确到秒的时间戳么?毫秒 13位 1278392014312
秒 10位 1278392014是否有直接将 1278392014 (10位的时间戳) 转成时间的方法?Timestamp 好想无法转换 1278392014 时间不对 只能再后面加 000 这样才能转换 但是个人感觉这样太麻烦了
但是 我数据库里面的只需要精确到秒就可以了这个可以节省3位的长度 Java 有办法获取到 只精确到秒的时间戳么?毫秒 13位 1278392014312
秒 10位 1278392014是否有直接将 1278392014 (10位的时间戳) 转成时间的方法?Timestamp 好想无法转换 1278392014 时间不对 只能再后面加 000 这样才能转换 但是个人感觉这样太麻烦了
解决方案 »
- 背景图片移动效果一直不理想 求帮忙
- 求排序法
- 哪位能使我明天不加班??无语的jspsmart上传组件!
- 关于UnsupportedClassVersionError异常
- 请分析一下这段JDNI方面的程序
- 想好好学JAVA,新手该学习些什么?
- 求教,如何让具有同样优先级的n个线程分时间片执行阿?
- 外部类范围之外的类要引用内部类时,是否要先构造外部类对象后才能构在内部类对象?
- 这都出错,我不想活了,大家看看
- cascade的定义?
- oracle查询中having和group by条件书写时先后顺序是什么?
- 要用java的io流编写一段代码:从键盘输入文字,存入文件,再读取出来,显示在控制台。但是我的代码,输入英文的时候没有错误,输入汉字的时候,读取显示的就成了乱码
不过我觉得正常情况下都是使用数据库的Date、DateTime(Timestamp)字段类型,不应该自己用int存储
* 将时间转换为显示字符串,“全长”格式 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;
}
别写什么除以一千,有现成的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);
}
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;
}
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);
now = now/1000 ;
取出来的时候 乘以 1000