您还未登录! 登录 | 注册 | 帮助  

您的位置: 首页 > 软件开发专栏 > 开发技术 > 正文

编程常用的几种时间戳转换(java .net 数据库)

发表于:2017-08-08 作者:小恩 来源:

   Java时间戳转换
/*
       * 将时间转换为时间戳     */   
      public static String dateToStamp(String s) throws ParseException{
          String res;
          SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          Date date = simpleDateFormat.parse(s);        long ts = date.getTime();
          res = String.valueOf(ts);        return res;
      }

    /*
       * 将时间戳转换为时间     */
      public static String stampToDate(String s){
          String res;
          SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        long lt = new Long(s);
          Date date = new Date(lt);
          res = simpleDateFormat.format(date);        return res;
      }
 
 .Net 时间戳转换
  将系统时间转换成UNIX时间戳
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970,1,1));
DateTime dtNow = DateTime.Parse(DateTime.Now.ToString());
TimeSpan toNow = dtNow.Subtract(dtStart);string timeStamp = toNow.Ticks.ToString();
timeStamp = timeStamp.Substring(0,timeStamp.Length - 7);
  将UNIX时间戳转换成系统时间
string timeStamp = "1144821796";
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970,1,1));long lTime = long.Parse(timeStamp + "0000000");
TimeSpan toNow = new TimeSpan(lTime);
DateTime dtResult = dtStart.Add(toNow);
  Oracle时间戳timestamp字段
  //插入操作
  INSERT INTO tablename(
  CUSTID,
  UPDATETIMESTAMP
  )VALUES(
  #{custid, jdbctype=VARCHAR},
  TO_TIMESTAMP(#{updatetimestamp},'syyyy-mm-dd hh24:mi:ss.ff')
  )
  //查询操作
  SELECT
  CUSTID,
  TO_CHAR(UPDATETIMESTAMP,'syyyy-mm-dd hh24:mi:ss.ff') AS UPDATETIMESTAMP
  FROM tablename
  SQLSERVER时间戳日期转时间戳
  CREATE FUNCTION [dbo].[UNIX_TIMESTAMP] (@ctimestamp datetime) RETURNS integer
  AS
  BEGIN
  /* Function body */
  declare @return integer
  SELECT @return = DATEDIFF(SECOND,{d '1970-01-01'}, @ctimestamp)
  return @return
  END
  select * ,dbo.[UNIX_TIMESTAMP](createdate) from 表