Thursday, February 23, 2017

Java Implementation ! Challenge 1 : Showing Current Time

What's up guys welcome back to my tutorial blog. Hope you guys enjoy learn programming from my blog these entire time.By the way in this new series from my blog I'm gonna show you guys the implemantation of Java programming languange. The reason why I make this Challenge Series is because I want you guys to know what you guys are learning about. No one feel comfortable while learning something that they don't know what is it for.

In this first Java Challenge series, I'm gonna show you guys how to show the current time in Greenwich Mean Time format (00:00;00). For example, the time when I write this post is 17:44:30. Also, in this Tutorial Series, I'm gonna use Notepad++ instead of eclipse, I also suggest you guys to do the same thing. Because whenever you guys are using an IDE like eclipse, you will find your error syntax faster than it should. While learning programming, the more time you spend on it, the more you will get used to it. I want you guys to spend more time in coding, because that's what great programmers did. As usual, I'm gonna show you guys the program first, I want you guys to read it carefully, then I'm gonna explain each part of the codes. Here we go.





You guys still remember how to compile a source code from notepad right ? If not, just take it easy. You still can check it out in Part 2 . After finish with compiling, I want you guys to run the program by typing "java TheCurrentTime" in the command line (Command Prompt) to run the program.
Here is the results :


Now, Let's Read the Codes !

Line 5 :

There, you can see a variable named "totalOfMiliSecond" with long datatypes.

Q : What is long datatypes ?

A : long is one of datatypes in programming which is another shape of int  datatypes. It holds the
      same types of values as the int datatypes did.

Q : What is the different between long and int ?

A : To determine which datatypes to use, you will have to know each function and limit range of each
      datatypes. the limit of int datatypes is  -2^31 to 2^31  and the limit of long datatypes is -2^63 to 
      (2^63)-1 . So, it depends on the size of the values.

Next I assign a values inside the variable. Maybe you guys are wondering why is the values like that. So int the System class which you guys always use to print out text (System.out.print() ) are containing a lot of methods inside it which have been included in java.lang (basic library of java) so you guys don't need to import it first as you guys did when you want to use the Scanner.

Q : okay, I conclude that System is a class which contain a lot of methods inside it. Then what is that
      "currentTimeMillis()" did ?

A : It's a basic method in java that whenever you use it, it will show you how long is the time from
      1 January 1970 until now 23 February 2017 (the time I make this post) in MIliSecond format
      (1/1000 second).

Q : Wow ! that's a lot of number of a values.

A : That's why we use long datatypes instead of int datatypes. Because the values are huge.

Now we have a variable that contains the times since 1 January 1970 until now in Mili Second format. Next we want to change it to Second format.

Line 7 :
In this line, we make a variable that contains the Times sinces 1 January 1970 until now in Second format. First you have to know that 1 second is equal 1000 milisecond. So if we have a milisecond
format, we have to divide it with 1000 to convert it to second format. For example, 1.000.000 mili seconds is equal 1000 seconds.

Line 9 :
In this line, we want to make a variable that contains the current time in second . In this case, the current second is 28. That's because totalOfSecond % 60 is equals 28.

Q : Why do you use % 60 ?

A : For example. I start eat at 06:00:00 AM. Then I spent 328 seconds to finish my meal. How do I
      know what time is it now in Second format ? so I calculate it with 328 % 60. The result is
      28. And because 328/60 in integer type is equals 5. So we know that I spent 5 minutes and 28
      seconds to finish my meals. So it's 06:05:28 AM right now. That's exactly what I did to determine
      the current minutes and hours on the Line 11 - 17. I don't have to tell you guys that 1 minute is
      equals 60 seconds and 1 hour is equal 60 minutes right ? do I ? hahaha.

Also, you guys must be wondering why I type (int) before the values. It is called type casting. Type Casting  is forcing the datatypes of a values into a specific datatypes that we want. In this case, totalOfMiliSeconds/1000 must be a very long number that I doubt it will be an integer number whenever I divide it with 1000. So I forced it into integer to round the values.

Line 17 :

Maybe you guys are wondering why I use % 24 to find the currentHour variable. Example, I start sleeping at 13:00:00 (I use 24 hours format this time). What time is it now if I slept for 55 hours ? To determine the hour, I'm gonna use 55%24 which is equals 7. So the current time is 13:00:00 + 7 hours
Which means that it is 20:00:00 right now. That's exactly what i did on the codes above to find out the current hour.

Challenge :

I want you guys to rewrite the codes above without cheating or peeking the codes above. Understand the code, don't memorize it ! Close or Minimize your browser first then go to your text editor to rewrite the codes. That's it for now guys I hope you guys are enjoying it. Thank you guys for visiting my blog and see you on the next Challenge Series ! Good Luck guys !

2 comments:

  1. you said this is for greenwich mean time format.. what is that mean?? that this code has a default hour from greenwich mean time? it is possible to print the current time in my country instead in greenwich?

    ReplyDelete
    Replies
    1. read more about GMT here buddy :
      https://en.wikipedia.org/wiki/Greenwich_Mean_Time
      In my knowledge, GMT is a universal time that were used long time ago by British Sailor to determine their location from "meridian Greenwich".
      and Yes, it's possible to print the current time from your country. But that's not the point of this content. Thank you very much for visiting my blog.

      Delete