Solved

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

Posted on 2007-04-08
4
212 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

948 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

18 Experts available now in Live!

Get 1:1 Help Now