Sunday, October 21, 2012

Continue Learning JAVA (a new approach)

From reading these blog entries, it's clear the trend is I am repeating myself quite often and not making any real progress. First there has not been a clear goal. Second, I find myself simply typing in or cut/paste from other sources. Sure these example programs are good in that it provides for repetition. With repetition, there  is some learning taking place. But not any depth. Third, there are specific programmatic weaknesses that I need to improve on. Understanding classes is one of these.

So what the go forward plan is stop using the example program method. Instead, (and I've already started) is to put together utility type classes, then call these from a test program. Figure lots of the basics will eventually coded into these utility classes and can be used over again. This will be helpful in that I'll have to code these only once and not get frustrated having to constantly find and re-code. An example is the dateUtils class.

As expected, this dateUtils class contains methods that return the time and date in various formats. These are most commonly used formats that I use are dd-MMM-yy,  hh:mm:ss  both combined, also POSIX time (number of seconds since UNIX epoch). One additional method accepts a hh:mm:ss and adds this to the current time. The return is the future time.

So in the test driver (uitest.java - the file with public static void main(String[] args) { ... }, I declare the following:


        dateUtils du = new dateUtils();
       
Calling each of the methods in the dateUtils file are done as follows:

        System.out.println(du.utilGetDateTime());
        System.out.println(du.utilDate());
        System.out.println(du.utilTime());
        System.out.println(du.utilEpoch());
        System.out.println(du.utilAddSecs(120));
        System.out.println(du.utilAddTime("12:34:56"));

Is this helpful? I think so. Of course there is a naming nomenclature irregularity as the class file is dateUtils.java and each method is called util(Something). It all works but a better naming convention may be in order. What has been helpful is all of the date / time formatting is done inside each of the methods so there was good experience gained.




No comments:

Post a Comment