public class TimeUtil {
public static string getCurrentTime()
{
return DateTime.Now.ToString ("yyyy_MM_dd_HH_mm_ss");
//DateTime.Now.ToString("yyyy-MM-dd hh_mm_ss")
}
public static string getCurrentTimeEx()
{
return DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss");
//DateTime.Now.ToString("yyyy-MM-dd hh_mm_ss")
}
public static string getCurrentTimeWithMilliSecond()
{
string res = "";
try
{
res = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss_fff");
}
catch (Exception e)
{
Debug.Log("时间异常getCurrentTimeWithMilliSecond:" + e);
}
return res;
}
public static string getCurrentTimeTight()
{
return DateTime.Now.ToString ("yyyyMMddHHmmss");
}
public static float getSecondsFromStartup()
{
return Time.realtimeSinceStartup;
}
public static string formatTime(float second)
{
//yyyy-MM-dd HH:mm:ss
//return new DateTime((long)(second * 10000000)).ToString("HH:mm:ss:ff");//毫秒:c#是fff,java是SSS
return new DateTime((long)(second * 10000000)).ToString("mm:ss:ff");//毫秒:c#是fff,java是SSS
}
public static string formatTime2(float second)
{
//yyyy-MM-dd HH:mm:ss
return new DateTime((long)(second * 10000000)).ToString("HH:mm:ss");//毫秒:c#是fff,java是SSS
//return new DateTime((long)(second * 10000000)).ToString("mm:ss:ff");//毫秒:c#是fff,java是SSS
}
/// <summary>
/// 获取时间戳
/// </summary>
/// <returns></returns>
public static string GetTimeStampSeconds()
{
TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalSeconds).ToString();
}
/// <summary>
/// 获取时间戳
/// </summary>
/// <returns></returns>
public static string GetTimeStampMilliseconds()
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalMilliseconds).ToString();
}
/// <summary>
/// 获取时间戳
/// </summary>
/// <returns></returns>
public static long GetTimeStampMillisecondsEx()
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalMilliseconds);
}
/// <summary>
/// 获取时间戳
/// </summary>
/// <returns></returns>
public static long GetTimeStampSecondsEx()
{
TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalSeconds);
}
/// <summary>
/// 时间戳[10|13]转为C#格式时间
/// </summary>
public static DateTime StampToDateTime(string stamp)
{
if (stamp.Length == 10 || stamp.Length == 13)
{
try
{
DateTime StartDateTime = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1,0,0,0,0, DateTimeKind.Utc), TimeZoneInfo.FindSystemTimeZoneById("China Standard Time"));
DateTime StartDateTime1;
if (stamp.Length == 10)
{
StartDateTime1 = StartDateTime.AddSeconds(long.Parse(stamp));
}
else
{
StartDateTime1 = StartDateTime.AddMilliseconds(long.Parse(stamp));
}
return StartDateTime1;
}
catch (Exception e)
{
return DateTime.Now;
}
}
else
{
return DateTime.Now;
}
}
/// <summary>
/// 获取从0001年1月1日0点到现在的100毫微秒数(实际就是100纳秒)
/// </summary>
/// <returns>The current ticks.</returns>
public static long getCurrentTicks()
{
return DateTime.Now.Ticks;
}
/// <summary>
/// 获取经历的时间,单位秒
/// </summary>
/// <returns>The time offset.</returns>
/// <param name="ticksFrom">Ticks from.</param>
/// <param name="ticksTo">Ticks to.</param>
public static float getTimeOffset(long ticksFrom, long ticksTo)
{
//Debug.Log("ticksTo-ticksFrom:"+(ticksTo-ticksFrom));
return (ticksTo - ticksFrom) / 10000000f;
}
}
常用时间tool
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 我们经常遇到,需要根据时间段来查询数据的需求,一般来说我们都是用的日期组件,但是有时候还需要设置默认值,这时候就需...
- 简介 本文记录了在JAVA开发工作中遇到了时间格式为: yyyy-MM-ddTHH:mm:ss.SSS+SSSS ...
- 转自:MySQL 取得两个时间相差的分钟数 及 常用时间函数 --取得相隔秒数SELECTUNIX_TIMESTA...
- 本网站记录了最全的各种JavaDEMO ,保证下载,复制就是可用的,包括基础的, 集合的, spring的, My...