What is the c# equivalent of a calendar in Java?

I am converting java to c# and need to convert code involving 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 editing because I should use two possible methods (system. Datetime and calendar)? (I remember problems in the Java universe)

Response summary for simple use, system Datetime is appropriate, and there is no Java date problem If the date is dialed forward between calls, there should be a call

Solution

System. The datetime structure is exactly what you need

Preferred method:

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

As Joel coehoorn points out, you can compress the code into one line I've become so addicted to implementation that I don't see what you actually want to do - fortunately Joel pointed it out

This will push all this into one phone It's pretty.

Direct translation (not recommended):

To convert java code to C #, you need to do the following:

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 view:

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));

Java problems about datetime

The datetime structure does not have the same problem as the date implementation of Java; So you shouldn't encounter the same problems that plague the Java world

Other methods

For example, another user pointed out, you can also use system Globalization. Calendar course I get along well with the datetime structure, and it's a little lighter than the calendar class, but they can all be used If you want to jump to the date and calendar implementation, please use the calendar class; If you want to stick to an implementation of date, the datetime structure is fine

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>