百科网

首页 > 科技数码

科技数码

教你时间戳如何转换为日期格式

科技数码佚名2023-01-17

时间戳如何转换为日期格式?当初把日期格式转换为时间戳是为了更好的记录数据,现在如果想要看看当初时间戳被转换的时间,可以按照以下方法来实现,详情请阅读下文MySQL、C#、JS时间戳转换方法。

MySQL、C#、JS时间戳转换方法:

一、MySQL戳转换方法:

1、原理:

时间戳的原理是把时间格式转为十进制格式,这样就方便时间的计算,如:1377216000000 转化后是 2013年08月23日 。

2、步骤:

(1) 创建 DateUtilsl类。

(2) 输入代码:

01importjava.text.ParseException;02importjava.text.SimpleDateFormat;03importjava.util.Date;04/*05* @author Msquirrel06*/07public class DateUtils {08privateSimpleDateFormat sf = null;09/*获取系统时间 格式为:"yyyy/MM/dd "*/10public static String getCurrentDate() {11Date d = newDate();12sf = newSimpleDateFormat("yyyy年MM月dd日");13returnsf.format(d);14}15/*时间戳转换成字符窜*/16public static String getDateToString(long time) {17Date d = newDate(time);18sf = newSimpleDateFormat("yyyy年MM月dd日");19returnsf.format(d);20}21/*将字符串转为时间戳*/22public static long getStringToDate(String time) {23sdf = newSimpleDateFormat("yyyy年MM月dd日");24Date date = newDate();25try{26date = sdf.parse(time);27} catch(ParseException e) {28// TODO Auto-generated catch block29e.printStackTrace();30}31returndate.getTime();32}复制代码importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Date;/** @author Msquirrel*/public class DateUtils {privateSimpleDateFormat sf = null;/*获取系统时间 格式为:"yyyy/MM/dd "*/public static String getCurrentDate() {Date d = newDate();sf = newSimpleDateFormat("yyyy年MM月dd日");returnsf.format(d);}/*时间戳转换成字符窜*/public static String getDateToString(long time) {Date d = newDate(time);sf = newSimpleDateFormat("yyyy年MM月dd日");returnsf.format(d);}/*将字符串转为时间戳*/public static long getStringToDate(String time) {sdf = newSimpleDateFormat("yyyy年MM月dd日");Date date = newDate();try{date = sdf.parse(time);} catch(ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}returndate.getTime();}