Java到C#Unix时间转换

时间:2014-03-07 16:37:43

标签: c# java unix-timestamp

我有这个Java代码,允许输入该特定格式的字符串并将其转换为Unix时间(long类型)。

string input = "2014-03-08T01:00:00-08:00";

Calendar cal = Calendar.getInstance();
SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");

(long) dateParser.parse(input.getTime())/1000

我想知道在C#中是否存在与此相同的参数?

1 个答案:

答案 0 :(得分:2)

试试这段代码。看看它是否有帮助。

using System;
using System.Globalization;

namespace ConsoleApplication 
{
    class Program
    {
        static void Main(string[] args)
        {
            string dateString, format;
            DateTime result;
            CultureInfo provider = CultureInfo.InvariantCulture;

            dateString = "2010-12-25T05:05:05.888";
            format = "yyyy-MM-dd'T'HH:mm:ss.fff";
            try
            {
                result = DateTime.ParseExact(dateString, format, provider);
                Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
                Console.WriteLine(result.Millisecond);
            }
            catch (FormatException fe)
            {
                Console.WriteLine(fe.StackTrace);
                Console.WriteLine("{0} is not in the correct format", dateString);
            }
        }
    }
}