在C#中,Java的SimpleDateFormat类相当于什么?(What is the equivalent of Java's SimpleDateFormat class in C#

编程入门 行业动态 更新时间:2024-10-27 16:30:14
在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.

更多推荐

本文发布于:2023-08-08 00:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466781.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:SimpleDateFormat   Java   class   equivalent

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!