Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 405267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:23:04+00:00 2026-05-12T17:23:04+00:00

I am converting Java to C# and need to convert code involving Calendar: Calendar

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T17:23:05+00:00Added an answer on May 12, 2026 at 5:23 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm converting some C# code to Java and I need to include an exception
I need to convert C code to Java. The minimal C code is: void
I am converting Java code to C# and need to replace the use of
I am manually converting code from Java (1.6) to C# and finding some difficulty
I need a tool for converting database tables to entity java files for hibernate.
i need help in converting sms in PDU mode to text mode in Java.
I am converting code from c# to java. I have the following xml structure:
I need some help converting a java byte array to a 7-Bit ASCII string.
Is there any tool that might help in converting the Java code written using
I am currently converting some OpenCV code from C++ to Java. I can't use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.