tags:

views:

445

answers:

3

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 EDIT As there are two possible approaches (System.DateTime and Calendar) which should I use? (I recall problems in the Java universe here)

SUMMARY of RESPONSES For simple uses System.DateTime is appropriate and does not have the problems of Java's Date. There should be a single call in case the date ticks forward between calls.

+5  A: 
System.DateTime

Like:

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

And so on.

Noon Silk
+6  A: 

The System.DateTime structure is what you're looking for.

Preferred Way:

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

As Joel Coehoorn points out, you could condense that code down to one line. 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:

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:

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

The DateTime structure doesn't have the same issues that Java had with their date implementation; so you shouldn't have the same problems that plagued the Java world.

Other Methods

As another user pointed out, you can use the System.Globalization.Calendar class as well. 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. If you're going to jump around date and calendar implemenations, then go with the Calendar class; if you're going to stick with one implementation of dates, then the DateTime struct is just fine.

George Stocker
Or, even better, condense all that to just DateTime.Now.AddMonths(1).ToString("MMddyyHHmm")
Joel Coehoorn
Seksi code . +1.
George Stocker
You should avoid repeatedly calling DateTime.Now and then playing with the date parts; what if the clock ticks over midnight between getting DateTime.Now.Day and DateTime.Now.Hour?
Roger Lipscombe
+1, very good point, Roger. In that vein I'll bump the 'update' up to the top as the 'preferred way' to do it.
George Stocker
+2  A: 

There's also Calendar.

Nick Veys
+1 I had forgotten about that!
George Stocker