java - How to write the follow simple DateUtil in C#(.net) ?
天蓬老师
天蓬老师 2017-04-17 14:49:27
0
2
886

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;
        }
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

Antworte allen(2)
刘奇
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;
    }
}
黄舟

oh~

try this tool Octopus .NET Translator
or this XES

if u can Over the wall~

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!