How to write the follow simple DateUtil in C#(.net) ?
The follow DateUtil is written in Java. How to write it in C#(.net), especially about the "Locale" ?
java的日期类和C#的日期类有哪些对应关系?求科普
/// <summary>
/// 日期工具类
/// </summary>
public class DateUtil
{
public const string DATE_PATTERN = "yyyy-MM-dd";
public const string DATE_PATTERN_SIMPLE = "yyyyMMdd";
public const string LOG_DATE_PATTERN = "dd/MMM/yyyy";
public const string HOUR_PATTERN = "yyyy-MM-dd-HH";
public const string MIN_PATTERN = "yyyy-MM-dd-HH-mm";
public const string SIMPLE_SECOND_PATTERN = "yyyyMMddHHmmss";
public const string COMMON_PATTERN = "yyyy-MM-dd HH:mm:ss";
public const string TIME_PATTERN = "HH:mm:ss";
public const string TIME_MIN_PATTERN = "HH:mm";
public static readonly Locale US = Locale.US;
public const string CHART_DATE_PATTERN = "%Y-%m-%d";
public const string CHART_HOUR_PATTERN = "%Y-%m-%d-%H";
private const int ONE = 1;
public static long yesterdayBeginTime()
{
DateTime calendar = new DateTime();
calendar.AddDays(-1);
calendar.set(DateTime.HOUR_OF_DAY, 0);
calendar.set(DateTime.MINUTE, 0);
calendar.set(DateTime.SECOND, 0);
calendar.set(DateTime.MILLISECOND, 0);
return calendar.TimeInMillis;
}
java .net c# transform datetime
秘神流人偶
9 years, 8 months ago
Answers
千梵是笨蛋
answered 9 years, 8 months ago
public class DateUtil
{
public const string DATE_PATTERN = "yyyy-MM-dd";
public const string DATE_PATTERN_SIMPLE = "yyyyMMdd";
public const string LOG_DATE_PATTERN = "dd/MMM/yyyy";
public const string HOUR_PATTERN = "yyyy-MM-dd-HH";
public const string MIN_PATTERN = "yyyy-MM-dd-HH-mm";
public const string SIMPLE_SECOND_PATTERN = "yyyyMMddHHmmss";
public const string COMMON_PATTERN = "yyyy-MM-dd HH:mm:ss";
public const string TIME_PATTERN = "HH:mm:ss";
public const string TIME_MIN_PATTERN = "HH:mm";
public static readonly TimeZoneInfo Us
= TimeZoneInfo.FindSystemTimeZoneById("Central America Standard Time");
// 可以通过 TimeZoneInfo.Create(...) 自建时区
// 下面这两个格式没搞懂
public const string CHART_DATE_PATTERN = "%Y-%m-%d";
public const string CHART_HOUR_PATTERN = "%Y-%m-%d-%H";
private const int ONE = 1;
public static long yesterdayBeginTime()
{
DateTime date = DateTime.Now;
date = date.AddDays(-1);
// date = new DateTime(date.Year, date.Month, date.Day, 0, 0, 0, 0);
// 如果只是为了取日期部分,可以直接取
date = date.Date;
// return date.Ticks; // 单位是 100纳秒,自0001年1月1日
// return date.Ticks / 10000; // 毫秒,自0001年1月1日
return ToJavaMilliseconds(date);
}
public static long ToJavaMilliseconds(DateTime value, TimeZoneInfo timezone = null)
{
DateTime date1970 = new DateTime(1970, 1, 1, 0, 0, 0);
date1970 = TimeZoneInfo.ConvertTimeFromUtc(date1970, timezone ?? TimeZoneInfo.Local);
return (value.Ticks - date1970.Ticks) / 10000;
}
}
defuse
answered 9 years, 8 months ago