[英]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());
您可以使用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。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.