I want to convert this java code to C# code
Can any one help me please ? thanks in advance
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
Calendar cal=Calendar.getInstance(TimeZone.getDefault());
Date dateGMT= cal.getTime();
I'm that expert with c# (had some background with it though..), but you may try this code if you want to get the current utc time:
DateTime dt= DateTime.UtcNow;//get the UTC/GMT time now...
//different time formats...
dt.ToString("HH:mm"); // 07:00 // 24 hour clock // hour is
//always 2 digits
dt.ToString("hh:mm tt"); // 07:00 AM // 12 hour clock // //hour
//is always 2 digits
dt.ToString("H:mm"); // 7:00 // 24 hour clock
dt.ToString("h:mm tt"); // 7:00 AM // 12 hour clock
Console.Write(dt); //print result or you can do something about the result..
I have tried to write the different format then print it. I hope I have helped you. Don't hesitate to comment if you have further questions
DateTime.UtcNow is one way.
Luckily .UtcNow (Coordinated Universal Time) is always GMT (excepting leap-seconds).
C# has an absolutely gigantic library. The best place to start is its documentation.
Related
What is the proper way to turn an integer of seconds into a formatted string of hh:mm:ss in Java?
For instance:
int Seconds = 650
String Time = 00:10:50
Right now I'm using this:
String Time = new SimpleDateFormat("hh:mm:ss").format(new Date((Seconds*1000)));
But this seems to tack on hours for no reason, and I'm guessing it's because I'm misusing Date or SimpleDateFormat, but I'm too inexperienced to know what's wrong. Or is there just a built in system for this that I don't know about.
EDIT: I should point out that I know I could use simple division to peel out the hours, then the remaining minutes, then the remaining seconds, and patch all three of those pieces into a string, but I was wondering if Java has a baked-in way to do this.
You can use TimeUnit class defined in java.util.concurrent package.
for eg you want to calculate hours:
long hours=TimeUnit.SECONDS.toHours(seconds);
similar methods are available for calulating days, hours, minutes.
but mind you this will give you direct conversion to hours, so you will end up having more than 24 hours. For a proper implementation you need to first calculate the days, the do the necessary maths and the give the remaining value for calculating hours. Lastly write a string as per your required format.
There is actually a (good) reason "to tack on hours".
Date(long date) constructor's JavaDoc:
Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.
So, if your JVM is not running in the GMT timezone you're off accordingly.
It's by design and logical, too:
new Date() is expected to be your current local time
new Date(0) is expected to be January 1, 1970, 00:00:00 GMT + local offset = local time
new Date(650*1000) is expected to be January 1, 1970, 00:10:50 GMT + local offset = local time
Try it the following way,
int seconds = 650;
long millis = seconds * 1000;
String format = String.format("%d:%d:%d",
TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis),
TimeUnit.MILLISECONDS.toSeconds(millis)
- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))
);
System.out.println(format);
your code is correct .but problem is this gives you the time relative to start position .think if you run following code
String Time = new SimpleDateFormat("EEEE,MMMM d,yyyy hh:mm,a").format(new Date((0)));
System.out.println(Time);
output>>Wednesday,December 31,1969 04:00,PM //this 04 will be reason to your undesired output later
i think that is the minimum time for positive seconds .think when you give milliseconds 0 java gives you day as Wednesday and 4 hours ,so starting hours of java time is not 0.
so when you run
following code
String Time = new SimpleDateFormat("hh:mm:ss").format(new Date((Seconds*1000)));
output>> 04:10:50 //you expect 00:10:50
because `starting time+seconds`
04:00:00 + 00:10:50
but starting minutes and seconds are 0 so you only have problem about hours
if you can subtract starting hours then you will get desired output.
joda time library has interval so you can use it .take a look at this question
Suppose there are two dates A(start time) & B(end time). A & B could be the time on the same day or even different day. My task is to show difference in seconds. Date format which i am using is
Date Format :: "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
For e.g.
start date :: "2011-11-16T14:09:23.000+00:00"
end date :: "2011-11-17T05:09:23.000+00:00"
Help is appreciated.
Use the Seconds class:
DateTime now = DateTime.now();
DateTime dateTime = now.plusMinutes(10);
Seconds seconds = Seconds.secondsBetween(now, dateTime);
System.out.println(seconds.getSeconds());
This piece of code prints out 600. I think this is what you need.
As further advice, explore the documentation of joda-time. It's pretty good, and most things are very easy to discover.
In case you need some help with the parsing of dates (It's in the docs, really), check out the related questions, like this:
Parsing date with Joda with time zone
The answer of #pcalcao will be best in most cases. Be aware that seconds will be rounded to an integer.
If you are interested in sub-seconds accuracy just substract the milliseconds:
double seconds = (now.getMillis() - dateTime.getMillis()) / 1000d;
Running the following code:
String s = "1914-07-20T00:00:00+0200";
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
System.out.println(df.parse( s ));
I get this result:
Sun Jul 19 23:34:52 EET 1914
(notice the date and hour)
It's probably one of those time adjustments that took place to get the sun to be overhead at noon. These took place around that time as the world got smaller and local time zones were being replaced by more universal ones. 25 minutes could well be such an adjustment. You would have to look at the record of time zone adjustments for eastern Europe from then to now.
An alternate way to check this is to put this in a loop and see it it works right in 1924 and 1934 and so forth until now. At some point, if its such an adjustment, it might start working and you could narrow it down to a particular time at which all times after work as expected and all times before don't. Though, to be sure, there could be a sequence of adjustments. 10 minutes here and 7 minutes there that add up the 25 minutes.
Note
This page (http://www.prokerala.com/travel/timezones/EET?mode=history) shows the GMT offset as 1.64444 in 1901 and 2 in 1920. That's 39:52 minutes and seconds.
This page (http://home.tiscali.nl/~t876506/TZworld.html) is about the global tz database and shows a LMT (local mean time) of 1:34:52 for Europe/Athens which is theoretically supposed to be +2:00. I'm not quite sure what that has to do with it but it seems a strange coincidence to be exactly what OP is seeing. (I also found that joda-time uses the LMT offset for any dates prior to the first entry in the "official" timezone data file.
First of all, I know I can use Calendar, but I want to understand this problem and learn to solve it.
I have a Date with the current date. I want to set hours, minutes and seconds to 0. I do it with this code:
current_date.setHours(0);
current_date.setMinutes(0);
current_date.setSeconds(0);
Something is going wrong because if I do that, the day gets decreased by 1, for example, if today is 31, the day gets set to 30.
why? How can it be solved with Date (without using Calendar, I want to learn how to solve this problem by the hard way). Thanks
Just guessing...
I guess it has to do with time-zone.
When you set HMS to 0 you are not setting them according to your time zone, but according to UTC (not 100% sure, but...). So the resulting date is not:
0:0:0 YOUR LOCAL
but
0:0:0 GMT
and that's a day before if you are on America.
Try to set minutes to -yourDate.getTimezoneOffset() (try with positive value if it doesn't work).
Look at the implementation of these methods. They use a gregorian calendar. The error might come from that interaction.
Note: The set methods are all deprecated since JDK 1.1 see
I just need a small help regarding the DateTime format in java.I am writing a simple chat application based on yahoo messanger,in which I will read the packet of yahoo messanger and display the chat messages.Now I want to display the time from the given header.In a particular article it is said the "timestamp" will be 0x477BBA61(decimal 1199290977) which means "Wed, 2 Jan 2008 16:22:57 GMT" .
I am trying to reveal how that decimal is converted to that particular date.I tried to write a simple java application to convert that and its giving some other time.
public static void main(String[] arg)
{
Calendar obj = Calendar.getInstance();
obj.setTimeZone(TimeZone.getTimeZone("GMT"));
obj.setTimeInMillis(1199290977l);
System.out.println( obj.get(Calendar.HOUR)+":"+obj.get(Calendar.MINUTE));
}
output:9:8
Can anybody help me with this?
Your value of 1199290977L is wrong. That's measuring in seconds since the Unix epoch (midnight on January 1st 1970 UTC) - you need to multiply it by 1000 to get milliseconds since the epoch.
You're also using Calendar.HOUR which is the 12-hour clock instead of Calendar.HOUR_OF_DAY which is the 24-hour clock. This code:
Calendar obj = Calendar.getInstance();
obj.setTimeZone(TimeZone.getTimeZone("GMT"));
obj.setTimeInMillis(1199290977000L);
System.out.println(obj.get(Calendar.HOUR_OF_DAY) + ":" +
obj.get(Calendar.MINUTE));
... prints 16:22.
However, you should definitely use the java.text.DateTimeFormat class instead of doing this yourself - or, ideally, use Joda Time instead.
Imho to proceed you need to know:
whether or not that number is milliseconds or not
what is the starting point (in java is January 1, 1970, 00:00:00 GMT)
The timezone is probably in seconds; try to multiply the value by 1000 to get the milliseconds which Calendar expects.
You need to use a SimpleDateFormat - look at the documentation, it is pretty easy to understand, plus it includes a lot of examples (so you don't need to search for "date format tutorial" or something like that :))
EDIT: Ooops, I missed the part where you are passing the time in seconds instead of milliseconds and the result time is wrong, I misinterpreted your question and thought that you want to just parse the time, Jon's answer is better :)