在C#中,Java的SimpleDateFormat类相当于什么?(What is the equivalent of Java's SimpleDateFormat class in C#?)
任何人都知道在C#中Java的SimpleDateFormat的等效类? 我不知道我是否可以使用C#中的自定义日期和时间格式字符串转换以下Java代码
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US); TimeZone tz = TimeZone.getDefault(); format.setTimeZone(TimeZone.getTimeZone("GMT")); str = format.format(new Date());Anyone knows an equivalent class of Java's SimpleDateFormat in C#? I wonder if i can convert the following java code using Custom Date and Time Format Strings in C#
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US); TimeZone tz = TimeZone.getDefault(); format.setTimeZone(TimeZone.getTimeZone("GMT")); str = format.format(new Date());最满意答案
您可以使用DateTime的ToString()方法,并指定您希望输出DateTime的自定义格式 。在这种情况下:
// US culture var usCulture = new CultureInfo("en-US"); // Get current UTC time. var utcDate = DateTime.UtcNow; // Change time to match GMT + 1. var gmt1Date = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(utcDate, "W. Europe Standard Time"); // Output the GMT+1 time in our specified format using the US-culture. var str = gmt1Date.ToString("ddd, dd MMM yyyy HH:mm:ss z", usCulture);请注意,.NET的等效EEE是ddd。
You can use DateTime's ToString() method and specify the custom format that you want the DateTime to be output in. In this case:
// US culture var usCulture = new CultureInfo("en-US"); // Get current UTC time. var utcDate = DateTime.UtcNow; // Change time to match GMT + 1. var gmt1Date = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(utcDate, "W. Europe Standard Time"); // Output the GMT+1 time in our specified format using the US-culture. var str = gmt1Date.ToString("ddd, dd MMM yyyy HH:mm:ss z", usCulture);Please note that .NET's equivalent of EEE is ddd.
更多推荐
发布评论