获得唯一标识符——时间戳、GUID
2016-08-07 11:20
260 查看
引言:
在项目中我们常常需要获得一些唯一标识,除了数据库的自增字段,我们还可以用时间戳和获取GUID的方式,现总结代码如下,以备后需。方法:
写好调用即可。(代码如下)
代码(C#版):
时间戳:/// <summary> /// 获得时间戳 /// </summary> public static string GetTimeStamp() { TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); return Convert.ToInt64(ts.TotalSeconds).ToString(); }
GUID:
/// <summary> /// 获得GUID串 /// </summary> /// <returns></returns> protected string getGUID() { long i = 1; foreach (byte b in Guid.NewGuid().ToByteArray()) i *= ((int)b + 1); return string.Format("{0:x}", i = DateTime.Now.Ticks); }
小结:
一些有用简单的代码,增加自己的代码量。
把积累做在平时,需要时高效工作。
相关文章推荐
- HDU 5806:NanoApe Loves Sequence Ⅱ
- HDU 5806 NanoApe Loves Sequence Ⅱ (尺取法)
- HDU5806 NanoApe Loves Sequence Ⅱ
- string、stringbuilder、stringbuffer区别
- 【次小生成树 或 最小生成树】The Unique MST poj 1679
- 【杭电 5805 8.6 BC B NanoApe Loves Sequence】
- HDU 5805:NanoApe Loves Sequence
- hdu 5805 NanoApe Loves Sequence
- iOS开发UI篇—直接使用UITableView Controller
- #114 Unique Paths
- #157 Unique Characters
- hdu 5806 NanoApe Loves Sequence Ⅱ
- HDU 5805 NanoApe Loves Sequence(思维)
- EarlGrey iOS自动化UI测试(集成篇)
- NanoApe Loves Sequence-待解决
- Vue.js双向数据绑定模板渲染
- POJ 2127 Greatest Common Increasing Subsequence
- HDU 5806 BestCoder Round #86 NanoApe Loves Sequence Ⅱ (尺取法)
- HDU 5805 NanoApe Loves Sequence
- 错误: Sys.WebForms.PageRequestManagerParserErrorException: 未能分析从服务器收到的消息。