教你时间戳如何转换为日期格式
|
时间戳如何转换为日期格式?当初把日期格式转换为时间戳是为了更好的记录数据,现在如果想要看看当初时间戳被转换的时间,可以按照以下方法来实现,详情请阅读下文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();} 3、在对应使用的地方调用: 01DateUtils.getCurrentDate(); //获取系统当前时间02DateUtils.getDateToString(时间戳); //时间戳转为时间格式03DateUtils.getStringToDate("时间格式");//时间格式转为时间戳.复制代码DateUtils.getCurrentDate(); //获取系统当前时间DateUtils.getDateToString(时间戳); //时间戳转为时间格式DateUtils.getStringToDate("时间格式");//时间格式转为时间戳. 二、C#时间戳转换方法: C#的代码(加入了闰年): 注:.Net的DateTime对象返回的是100纳秒的时间单位,年份是从AD1开始计算的。 01class Program02{03// 定义必须变量04const int _1M = 60; // 分钟05const int _1H = _1M * 60; // 小时06const int _1D = _1H * 24; // 天07const long _1Y = _1D * 365; // 年(非闰年)08const long _YS = _1Y * 3 _1D * 366; // 一个闰年年度09const long _30D = _1D * 30; // 30天(月)10const long _31D = _1D * 31; // 31天(月)11const long _28D = _1D * 28; // 28天(月)12const long _29D = _1D * 29; // 29天(月)13long[] NormalYear = { _31D, _28D, _31D, _30D, _31D, _30D, _31D, _31D, _30D, _31D, _30D, _31D }; // 年14long[] LeapYear = { _31D, _29D, _31D, _30D, _31D, _30D, _31D, _31D, _30D, _31D, _30D, _31D }; // 闰年15static void Main(string[] args)16{17Program P = new Program();18System.Console.WriteLine(P.getDate(P.getTimeSpame()));19DateTime T = DateTime.Now;20System.Console.WriteLine(P.getTimeSpame() " : " P.getTimeSpame(T.Year, T.Month, T.Day, T.Hour, T.Minute, T.Second));21System.Console.ReadKey();22}23private Program() {}24public string getDate(long TimeSp)25{26// 年,月,天,小时,分钟,秒27int year = 0;28int month = 0;29int day = 0;30int hour = 0;31int minute = 0;32int second = 0;33//DateTime now = DateTime.Now;34//long TimeSp = getTimeSpame(); // 当前时间戳35// 年36int _y1 = (int)(TimeSp / _YS); // 获得按年度得到的年度37TimeSp -= _YS * _y1; // 计算剩余秒38int _y2 = (int)(TimeSp / _1Y); // 剩余年39TimeSp -= _1Y * _y2;40year = _y1 * 4 _y2 1970;41// 月42long[] YearArr = isLeapYear(year) ? LeapYear : NormalYear; // 获取年的月度表43month = 1; // 从1月开始计算44for (int i = 0; i < YearArr.Length; i )45{46if (TimeSp - YearArr[i] < 0) break;47 month;48TimeSp -= YearArr[i];49}50// 天51day = (int)(TimeSp / _1D);52TimeSp -= day * _1D;53// 时54hour = (int)(TimeSp / _1H);55TimeSp -= hour * _1H;56// 分57minute = (int)(TimeSp / _1M);58// 秒59second = (int)(TimeSp % _1M);60string DateStr = year "年" month "月" day "日 " hour "点" minute "分" second "秒";61return DateStr;62}63// 判断是否闰年64private bool isLeapYear(int year)65{66return (year % 4 == 0 ? true : false);67}68// 获取当前时间戳 按1970年开始计算,精度为秒!69private long getTimeSpame()70{71DateTime _Now = DateTime.Now;72DateTime _1970 = new DateTime(1970, 1, 1);73long _Sp = (_Now.Ticks - _1970.Ticks) / 10000000;74return _Sp;75}76// 按既定格式把时间转成成时间戳77private long getTimeSpame(int Year, int Month, int Day, int Hour, int Minute, int Second)78{79long val = 0;80val = Second; // 秒81val = Minute * _1M; // 分钟82val = Hour * _1H; // 小时83val = Day * _1D; // 天84long[] YearArr = isLeapYear(Year) ? LeapYear : NormalYear;85for (int i = 0; i < Month - 1; i )86{87val = YearArr[i];88}89Year -= 1970;90val = (Year / 4) * _YS;91Year -= (int)(Year / 4) * 4;92val = Year * _1Y;93return val;94}95}复制代码class Program{// 定义必须变量const int _1M = 60; // 分钟const int _1H = _1M * 60; // 小时const int _1D = _1H * 24; // 天const long _1Y = _1D * 365; // 年(非闰年)const long _YS = _1Y * 3 _1D * 366; // 一个闰年年度const long _30D = _1D * 30; // 30天(月)const long _31D = _1D * 31; // 31天(月)const long _28D = _1D * 28; // 28天(月)const long _29D = _1D * 29; // 29天(月)long[] NormalYear = { _31D, _28D, _31D, _30D, _31D, _30D, _31D, _31D, _30D, _31D, _30D, _31D }; // 年long[] LeapYear = { _31D, _29D, _31D, _30D, _31D, _30D, _31D, _31D, _30D, _31D, _30D, _31D }; // 闰年static void Main(string[] args){Program P = new Program();System.Console.WriteLine(P.getDate(P.getTimeSpame()));DateTime T = DateTime.Now;System.Console.WriteLine(P.getTimeSpame() " : " P.getTimeSpame(T.Year, T.Month, T.Day, T.Hour, T.Minute, T.Second));System.Console.ReadKey();}private Program() {}public string getDate(long TimeSp){// 年,月,天,小时,分钟,秒int year = 0;int month = 0;int day = 0;int hour = 0;int minute = 0;int second = 0;//DateTime now = DateTime.Now;//long TimeSp = getTimeSpame(); // 当前时间戳// 年int _y1 = (int)(TimeSp / _YS); // 获得按年度得到的年度TimeSp -= _YS * _y1; // 计算剩余秒int _y2 = (int)(TimeSp / _1Y); // 剩余年TimeSp -= _1Y * _y2;year = _y1 * 4 _y2 1970;// 月long[] YearArr = isLeapYear(year) ? LeapYear : NormalYear; // 获取年的月度表month = 1; // 从1月开始计算for (int i = 0; i < YearArr.Length; i ){if (TimeSp - YearArr[i] < 0) break; month;TimeSp -= YearArr[i];}// 天day = (int)(TimeSp / _1D);TimeSp -= day * _1D;// 时hour = (int)(TimeSp / _1H);TimeSp -= hour * _1H;// 分minute = (int)(TimeSp / _1M);// 秒second = (int)(TimeSp % _1M);string DateStr = year "年" month "月" day "日 " hour "点" minute "分" second "秒";return DateStr;}// 判断是否闰年private bool isLeapYear(int year){return (year % 4 == 0 ? true : false);}// 获取当前时间戳 按1970年开始计算,精度为秒!private long getTimeSpame(){DateTime _Now = DateTime.Now;DateTime _1970 = new DateTime(1970, 1, 1);long _Sp = (_Now.Ticks - _1970.Ticks) / 10000000;return _Sp;}// 按既定格式把时间转成成时间戳private long getTimeSpame(int Year, int Month, int Day, int Hour, int Minute, int Second){long val = 0;val = Second; // 秒val = Minute * _1M; // 分钟val = Hour * _1H; // 小时val = Day * _1D; // 天long[] YearArr = isLeapYear(Year) ? LeapYear : NormalYear;for (int i = 0; i < Month - 1; i ){val = YearArr[i];}Year -= 1970;val = (Year / 4) * _YS;Year -= (int)(Year / 4) * 4;val = Year * _1Y;return val;}} 三、JS时间戳转换方法: 代码如下: 01// 定义常量02var _1M = 60; // 分钟03var _1H = _1M * 60; // 小时04var _1D = _1H * 24; // 天05var _1Y = _1D * 365; // 年(非闰年)06var _YS = _1Y * 3 _1D * 366; // 一个闰年年度07var _30D = _1D * 30; // 30天(月)08var _31D = _1D * 31; // 31天(月)09var _28D = _1D * 28; // 28天(月)10var _29D = _1D * 29; // 29天(月)11var NormalYear = [ _31D, _28D, _31D, _30D, _31D, _30D, _31D, _31D, _30D, _31D, _30D, _31D ]; // 年12var LeapYear = [ _31D, _29D, _31D, _30D, _31D, _30D, _31D, _31D, _30D, _31D, _30D, _31D ]; // 闰年13var Now = new Date();14TimeSp = Now.getTime() / 1000;15//alert(Now.getTimezoneOffset()); // 时区差16TimeSp = -1 * Now.getTimezoneOffset() * _1M; // 修正UTC17// 年,月,天,小时,分钟,秒18var year = month = day = hour = minute = second = 0;19// 年20var _y1 = parseInt(TimeSp / _YS); // 获得按年度得到的年度21TimeSp -= _YS * _y1; // 计算剩余秒22var _y2 = parseInt(TimeSp / _1Y); // 剩余年23TimeSp -= _1Y * _y2;24year = _y1 * 4 _y2 1970;25// 月26var YearArr = year % 4 == 0 ? LeapYear : NormalYear; // 获取年的月度表27month = 1; // 从1月开始计算28for (i=0; i 注:JS的Date对象的getTime()方法返回的是UTC的时间戳,可以使用getTimezoneOffset()的方法来返回与UTC时差的分钟。 以上代码便是MySQL、C#、JS的时间戳转换方法介绍,因为闰年的存在,代码的使用前请阅读一下。 |
相关文章
热销商品
淘月子帽春秋产后防风保暖堆堆帽孕妇睡帽套头护额头帽产妇帽头巾女
月子帽春秋产后防风保暖堆堆帽孕妇睡帽套头护额头帽产妇帽头巾女
¥16.5 领券购买
淘产妇双层月子帽四季纯棉月子发带防风包头帽孕妇帽产后坐月子头巾
产妇双层月子帽四季纯棉月子发带防风包头帽孕妇帽产后坐月子头巾
¥16.5 领券购买
淘可爱纯棉月子帽春季3月份a类产后孕妇产妇帽头巾发带睡觉大头围帽
可爱纯棉月子帽春季3月份a类产后孕妇产妇帽头巾发带睡觉大头围帽
¥18.5 领券购买
天大头围月子帽女4月份产后纯棉薄款孕妇睡觉可戴防风春夏季产妇帽
大头围月子帽女4月份产后纯棉薄款孕妇睡觉可戴防风春夏季产妇帽
¥25.5 领券购买
天嫚熙月子帽春夏季丝柔棉产妇帽子产后纯棉3月份防风护头孕妇帽女
嫚熙月子帽春夏季丝柔棉产妇帽子产后纯棉3月份防风护头孕妇帽女
¥58.9 领券购买
天熊猫T-37全波段老人新款收音机专用老年老式复古怀旧充电款半导体
熊猫T-37全波段老人新款收音机专用老年老式复古怀旧充电款半导体
¥98 领券购买
天熊猫T-15收音机老人专用全波段新款半导体老年人老式调频短波便携
熊猫T-15收音机老人专用全波段新款半导体老年人老式调频短波便携
¥125 领券购买
天索贝纳英语四六级听力收音机大学考试专用FM调频专八4级6级接收机
索贝纳英语四六级听力收音机大学考试专用FM调频专八4级6级接收机
¥29.9 领券购买
天喜马拉雅播放器新款收音机老人专用充电款2026年陪伴机新年礼物
喜马拉雅播放器新款收音机老人专用充电款2026年陪伴机新年礼物
¥649 领券购买
天【羊毛】雅戈尔秋季官方新款TR西裤商务通勤男士中青年高级长裤子
【羊毛】雅戈尔秋季官方新款TR西裤商务通勤男士中青年高级长裤子
¥389 领券购买
淘【林秋楠同款】haonanhuang立褶微喇抗皱弹力西裤男女
【林秋楠同款】haonanhuang立褶微喇抗皱弹力西裤男女
¥289 领券购买
淘【1930s 600g重50羊毛】CULTUM好莱坞裤免熨烫复古双褶直筒西裤男
【1930s 600g重50羊毛】CULTUM好莱坞裤免熨烫复古双褶直筒西裤男
¥439 领券购买
天【新香首发】佩枪朱丽叶朱丽叶女士香水礼盒持久留香花果香调正品
【新香首发】佩枪朱丽叶朱丽叶女士香水礼盒持久留香花果香调正品
¥781 领券购买
淘adidas/阿迪达斯 Grand Court Alpha 减震透气防滑耐磨网球鞋板鞋
adidas/阿迪达斯 Grand Court Alpha 减震透气防滑耐磨网球鞋板鞋
¥318 领券购买
淘FILA斐乐儿童轻薄羽绒服2025冬新款童装小童网球上衣连帽宝宝外套
FILA斐乐儿童轻薄羽绒服2025冬新款童装小童网球上衣连帽宝宝外套
¥591 领券购买
天恒源祥女士纯棉抗菌舒适吸汗透气夏季运动休闲百搭中筒袜黑白棉袜
恒源祥女士纯棉抗菌舒适吸汗透气夏季运动休闲百搭中筒袜黑白棉袜
¥29.9 领券购买

