Solved

Java to C# - 500 pts.  less than 15 lines

Posted on 2007-04-08
4
210 Views
Last Modified: 2010-04-15
Please help translate these few lines of java code into C# ...

//Gets a calendar using the default time zone and locale. Calendar returned is based on the current time in the default time zone with the default locale.
Calendar cal = Calendar.getInstance();
long time = 1036074738312;
int day = 31;

cal.setTimeInMillis(time);                                      // Sets this Calendar's current time from the given long value.  long millis param is the new time in UTC milliseconds from the epoch.
if (cal.get(Calendar.DAY_OF_MONTH) == day) {                  
       ...  do some stuff here ...
}
0
Comment
Question by:lblinc
  • 2
  • 2
4 Comments
 
LVL 3

Expert Comment

by:kingtam2000
ID: 18874041
From what I can judge, you could use DateTime instead of Calender and use the constructor, after converting the time in milliseconds into 100-nanosecond units to form what you want:
            long time = 1036074738312;
            int day = 31;
            DateTime cal = new DateTime(time * 10000, DateTime.Now.Kind);
            if (cal.Day == day)
            {
                //Do Stuff....
            }

Hope that helps, if you have any questions, just post a comment
0
 

Author Comment

by:lblinc
ID: 18874286
Using time = 1036074738312 above, it appears is returning a year of  0033,  when actually i believe that 1036074738312 represents a day (31) from the year of 2003.  

What about adding the basetime to your DateTime cal  in order  to account for the time before 1970 ?  

DateTime baseTime = new DateTime(1970, 1, 1, 0, 0, 0);

Can you please consider that in your answer?     Thanks.
0
 
LVL 3

Accepted Solution

by:
kingtam2000 earned 500 total points
ID: 18874294
Yes, I realised that the time would return a year of 0033, but I wasn't sure where you wanted to start from, as the epoch was slightly ambiguous.  But to account for the epoch, I would add the following line after the datetime constructor:
            cal = cal.AddYears(1970);
So, in full:
            long time = 1036074738312;
            int day = 31;
            DateTime cal = new DateTime(time * 10000, DateTime.Now.Kind);
            cal = cal.AddYears(1970);
            if (cal.Day == day)
            {
                //Do Stuff....
            }

Sorry about that, didn't check the epoch in terms of Java, if you have any further questions, just post another comment.
0
 

Author Comment

by:lblinc
ID: 18874338
Excellent.   Works perfect.   ;))
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now