Monday, March 10, 2008

System Property

How to retrieve System properties ?.
What are the properties has to be used while doing cross platform coding ?

We have to make use of almost all the properties comes from System.getProperties method for our development. While doing cross platform coding File.separator and path.separator and encoding are very well useful for us to get to know correct value.

Code Snippets to list out all the properties:


Properties props = System.getProperties();
Iterator it = (props.entrySet().iterator());
while (it.hasNext()) {
Map.Entry property = 
(Map.Entry<String, String>) it.next();
System.out.println(property.getKey() 
+ ": " + property.getValue());
}

In my machine, I got following properties key are set


java.runtime.name
sun.boot.library.path
java.vm.version
java.vm.vendor
java.vendor.url
path.separator
java.vm.name
file.encoding.pkg
sun.java.launcher
user.country
sun.os.patch.level
java.vm.specification.name
user.dir
java.runtime.version
java.awt.graphicsenv
java.endorsed.dirs
os.arch
java.io.tmpdir
line.separator
java.vm.specification.vendor
user.variant
os.name
sun.jnu.encoding
java.library.path
java.specification.name
java.class.version
sun.management.compiler
os.version
user.home
user.timezone
java.awt.printerjob
file.encoding
java.specification.version
java.class.path
user.name
java.vm.specification.version
java.home
sun.arch.data.model
user.language
java.specification.vendor
awt.toolkit
java.vm.info
java.version
java.ext.dirs
sun.boot.class.path
java.vendor
file.separator
java.vendor.url.bug
sun.io.unicode.encoding
sun.cpu.endian
sun.desktop
sun.cpu.isalist

Time Difference Calculator from Long data type

I want to get to know the time difference from two date/time. In JAVA, there is no method available built in to find out the differences between two dates. Hence, we have to getTime in long format of first date and it should be subtracted with another date. Using this long value, we can form the date format.

Here in the below code snippets, start and end time caputed using System.currentTimeMillis(), we have to come to know how long it take to complete our business.


long startTime = System.currentTimeMillis();
//do your business
...
long endTime= System.currentTimeMillis();

//calculate difference
long timeDiff_long = endTime - startTime;

long days = timeDiff_long / (60 * 60 * 24);
long backdays = days * 60 * 60 * 24;

long hour = (timeDiff_long - backdays) / (60 * 60);
long backhour = hour * 60 * 60;

long minute = (timeDiff_long - (backdays + backhour) ) / 60;
long backminute = minute * 60;

long seconds = (timeDiff_long - (backdays + backhour + backminute )) ;
System.out.println("DAYS: " + days);
System.out.println("HOUR: " + hour);
System.out.println("MINUTE: " + minute);
System.out.println("SECOND: " + seconds);

Using the above code, we can format Date object as our wish.

Recent Posts

Unix Commands | List all My Posts

Texts

This blog intended to share the knowledge and contribute to JAVA Community such a way that by providing samples and pointing right documents/webpages. We try to give our knowledege level best and no guarantee can be claimed on truth. Copyright and Terms of Policy refer blogspot.com