简体   繁体   English

Java中Calendar的C#等效项是什么?

[英]what is the c# equivalent of Calendar in java?

I am converting Java to C# and need to convert code involving Calendar: 我正在将Java转换为C#,并且需要转换涉及Calendar的代码:

Calendar rightNow = Calendar.getInstance();
StringBuilder sb = new StringBuilder();
sb.append((rightNow.get(Calendar.MONTH) + 1));
sb.append(rightNow.get(Calendar.DAY_OF_MONTH));
sb.append(rightNow.get(Calendar.YEAR)).substring(2);
sb.append(rightNow.get(Calendar.HOUR_OF_DAY));
sb.append(rightNow.get(Calendar.MINUTE));

MORE EDIT As there are two possible approaches (System.DateTime and Calendar) which should I use? 更多编辑由于应该使用两种可能的方法(System.DateTime和Calendar)? (I recall problems in the Java universe here) (我在这里回顾了Java宇宙中的问题)

SUMMARY of RESPONSES For simple uses System.DateTime is appropriate and does not have the problems of Java's Date. 响应摘要对于简单使用,System.DateTime是合适的,并且没有Java的Date问题。 There should be a single call in case the date ticks forward between calls. 应该有一个通话,以防日期在通话之间滴答作响。

The System.DateTime structure is what you're looking for. 您正在寻找System.DateTime结构。

Preferred Way: 首选方式:

sb.Append(DateTime.Now.AddMonths(1).ToString("MMddyyHHmm"));

As Joel Coehoorn points out, you could condense that code down to one line. 正如Joel Coehoorn指出的那样,您可以将该代码压缩为一行。 I had become so engorged on the implementation, I didn't see what you were actually trying to do -- luckily Joel pointed it out. 我对实现变得如此着迷,没有看到您实际上要做什么—幸运的是,乔尔指出了这一点。

That will roll all of those up into one call. 这会将所有这些汇总为一个呼叫。 Pretty nifty. 很漂亮

Direct Translation (Not recommended): 直接翻译(不推荐):

To translate your Java code into C#, you'd do something like the following: 要将Java代码转换为C#,您需要执行以下操作:

string year = DateTime.Now.Year.ToString();
sb.Append(DateTime.Now.AddMonths(1));
sb.Append(DateTime.Now.Day);
sb.Append(year.Substring(2));
sb.Append(DateTime.Now.Hour);
sb.Append(DateTime.Now.Minute);

You can copy/paste this C# code to see: 您可以复制/粘贴此C#代码以查看:

StringBuilder sb = new StringBuilder();
string year = DateTime.Now.Year.ToString();
sb.Append(String.Format("Next Month is: {0} \n ",DateTime.Now.AddMonths(1)));
sb.Append(String.Format("Day is {0}\n ", DateTime.Now.Day));
sb.Append(String.Format("Year is {0}\n ", year.Substring(2)));
sb.Append(String.Format("The Hour is {0}\n ", DateTime.Now.Hour)); //getting late
sb.Append(String.Format("The Minute is {0}\n ", DateTime.Now.Minute));

Regarding Java issues with DateTime 关于DateTime的Java问题

The DateTime structure doesn't have the same issues that Java had with their date implementation; DateTime结构没有与Java的日期实现相同的问题; so you shouldn't have the same problems that plagued the Java world. 因此,您不应遇到困扰Java世界的相同问题。

Other Methods 其他方法

As another user pointed out , you can use the System.Globalization.Calendar class as well. 正如另一个用户指出的那样 ,您也可以使用System.Globalization.Calendar类。 I get along just fine with the DateTime struct, and it's a little lighter-weight than the Calendar class, but they both can be used. 我与DateTime结构很好地相处,并且比Calendar类轻一些,但是它们都可以使用。 If you're going to jump around date and calendar implemenations, then go with the Calendar class; 如果您打算跳过日期和日历的实现,请使用Calendar类; if you're going to stick with one implementation of dates, then the DateTime struct is just fine. 如果您要坚持使用日期的一种实现,那么DateTime struct就可以了。

System.DateTime

Like: 喜欢:

DateTime now = DateTime.Now;
DateTime tommorowsTomorrow = now.AddDays(2);

And so on. 等等。

还有Calendar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM