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

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

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

모든 응답(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~

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!