Showing posts with label tips. Show all posts
Showing posts with label tips. Show all posts

Tuesday, May 27, 2008

Perhitungan Durasi Eksekusi

Sering sekali kita ingin mengukur performansi dari aplikasi Java kita berdasarkan durasi eksekusi aplikasi tersebut.

Untuk itu saya menulis tips ini dalam bentuk satu source code, dimana kita menghitung durasi waktu dalam mili detik dan kemudian dikonversi ke satuan detik.

Dalam contoh ini, operasi yang dilakukan adalah pembacaan isi folder temp.


package org.phiintegration.com;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;

/**
* Ini adalah contoh perhitungan durasi waktu yang diperlukan
* untuk menjalankan keseluruhan aplikasi di thread Main.
*
* Dalam contoh ini kita melakukan perhitungan durasi waktu
* yang diperlukan untuk membaca daftar folder di temporary folder.
*
* @author Feris Thia
*
*/

public class DurasiBrowsingFolder {
public static void main(String[] args) throws IOException, Exception {

//Mencatat waktu awal proses
long waktuMulai= System.currentTimeMillis();

//Proses membaca daftar file di lokasi folder temp Java
String curDir = System.getProperty("java.io.tmpdir");
File dir = new File(curDir);

String[] entriFolder = dir.list();
if (entriFolder != null) {
for (int i=0; i<entriFolder.length; i++) {
String namaFile = entriFolder[i];
System.out.println(namaFile);
}
}


//Akhir proses

//Mencatat waktu akhir proses
long waktuSelesai = System.currentTimeMillis();

//Menghitung durasi dalam detik dan
//mencetak dengan format ##0.000000
double newRunTime = (double)(waktuSelesai - waktuMulai) / 1000;
DecimalFormat runtimeDF = new DecimalFormat("##0.000000");

System.out.println("Durasi eksekusi : " +
runtimeDF.format(newRunTime) +
" detik");
}
}

Tuesday, May 20, 2008

Video : GData Java Client API Demo

Here is the video showing a demo on using CellDemo class from Google Data (GData) Java client API bundled.


With this video I intended to express 3 things :
  1. How we execute the sample in Eclipse environment
  2. The running class will create a HTTPS channel
  3. Execution result is immediately visible to us
You can watch the video here.

Monday, March 3, 2008

Working with Date and Time in Java

I recalled when I started to learn Java, one of the burden I'm facing with is how to deal with date and time properly.

So to help anyone with the same problem, in this article I list down a number of "shortcut tips" on dealing with them.

1. Returning timestamp
Date now = new Date();

long nowLong = now.getTime(); //returning a timestamp
System.out.println("Value is " + nowLong);

2. Formatting date
SimpleDateFormat df = new java.text.SimpleDateFormat("MM/dd/yyyy");
Date now = new Date();
long nowLong = now.getTime(); //returning a timestamp

System.out.println(df.format(now));

3. Assigning date value with a formatted text literal
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");

try {
Date date = (Date) sdf.parse("12/31/2007");
System.out.println("Current date is : " + sdf.format(date));
} catch (ParseException e) {
System.out.print("Illegal date !");
}

4. Comparing date with compareTo() method of Date object
//With compareTo() method we should have a return of value -1, 0, and 1.
//Syntax : date1.compareTo(date2);
//Return value
//   -1 : date1 is earlier than date2
//    0 : date1 is having exactly same value with date2
//    1 : date2 is later than date2

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");

try {
Date date1 = (Date) sdf.parse("12/31/2007");
Date date2 = (Date) sdf.parse("12/31/2008");
Date date3 = (Date) sdf.parse("12/31/2008");

System.out.println(date1.compareTo(date2));
System.out.println(date2.compareTo(date1));
System.out.println(date2.compareTo(date3));
} catch (ParseException e) {
System.out.print("Illegal date !");
}

5. Assigning date with GregorianCalendar class
//Get current date and time
Calendar cal = new GregorianCalendar();
System.out.println(cal);

//Set date with int value
Calendar newdate = new GregorianCalendar(2008, Calendar.MARCH, 1);
System.out.println(newdate);

//Convert into Date object
Date date = newdate.getTime();     
System.out.println(date);

//Print with a MM/dd/yyyy format
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
System.out.println("New date is : " + sdf.format(date));

Online Resources:
1. http://www.javaworld.com/jw-12-2000/jw-1229-dates.html
2. http://www.javaworld.com/javaworld/jw-03-2001/jw-0330-time.html
3. http://forum.java.sun.com/thread.jspa?threadID=552315&messageID=2701323
4. http://forum.java.sun.com/thread.jspa?threadID=539302&range=1&start=2&forumID=31
5. http://www.exampledepot.com/