c#中与Java' SimpleDateFormat类等价的是什么?

本文关键字:是什么 SimpleDateFormat Java 中与 | 更新日期: 2023-09-27 18:14:28

有人知道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());

c#中与Java' SimpleDateFormat类等价的是什么?

您可以使用DateTimeToString()方法,并指定您希望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。