2014-03-07 23 views
0

我有這個Java代碼,允許輸入特定格式的字符串並將其轉換爲Unix時間(類型爲long)。Java到C#Unix時間轉換

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#中是否存在與此相同的參數?

+0

'cal'似乎未被使用。 – Haney

回答

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); 
      } 
     } 
    } 
} 
+0

你好,彼得。看來這隻會在我使用'2010-12-25T05:05:05.888'作爲例子時返回一個3位數的數字。什麼屬性會返回這樣的格式:http://puu.sh/7mn7D/4a850f37f6.png – theGreenCabbage

+0

我認爲這解決了我的問題:http://stackoverflow.com/a/7596509/1913389 – theGreenCabbage

+0

嘿@peter - 我剛剛注意到'2014-03-10T08:00:00-07:00'中'-07:00'的結尾數字表示時區。任何想法如何處理它並將其包含在轉換中? – theGreenCabbage