Class lấy về giá trị ngày giờ hệ thống


package Function;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

/**
*
* @author Nguyen Cong Truong
*/
public class DateTimeMaster {

public static String getHour() {
Calendar cal = new GregorianCalendar();
return cal.get(cal.HOUR_OF_DAY) < 10 ? "0" + cal.get(cal.HOUR_OF_DAY) : "" + cal.get(cal.HOUR_OF_DAY);
}

public static String getMinute() {
Calendar cal = new GregorianCalendar();
return cal.get(cal.MINUTE) < 10 ? "0" + cal.get(cal.MINUTE) : "" + cal.get(cal.MINUTE);
}

public static String getSecond() {
Calendar cal = new GregorianCalendar();
return cal.get(cal.SECOND) < 10 ? "0" + cal.get(cal.SECOND) : "" + cal.get(cal.SECOND);
}

public static String getDate() {
Calendar cal = new GregorianCalendar();
return cal.get(cal.DAY_OF_MONTH) < 10 ? "0" + cal.get(cal.DAY_OF_MONTH) : "" + cal.get(cal.DAY_OF_MONTH);
}

public static String getMonth() {
Calendar cal = new GregorianCalendar();
return (1 + cal.get(cal.MONTH)) < 10 ? "0" + (1 + cal.get(cal.MONTH)) : "" + (1 + cal.get(cal.MONTH));
}

public static String getYear() {
Calendar cal = new GregorianCalendar();
return cal.get(cal.YEAR) + "";
}

public static String getCurrentDateTimeWithDotSeprator() {
return getHour() + "." + getMinute() + "-" + getDate() + "." + getMonth() + "." + getYear();
}

public static String getCurrentDateTime() {
return getHour() + ":" + getMinute() + "-" + getDate() + "/" + getMonth() + "/" + getYear();
}

public static String getCurrentDate() {
return getDate() + "/" + getMonth() + "/" + getYear();
}

public static String getCurrentDateWithDotSeprator() {
return getDate() + "." + getMonth() + "." + getYear();
}

/**
*
* @param date is dd/mm/yyyy
* @return
*/
public static boolean isValidDate(String date) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyy");
sdf.setLenient(false);
sdf.parse(date);
} catch (ParseException e) {
return false;
} catch (IllegalArgumentException e) {
return false;
}
return true;
}
public static void main(String args[]){
System.out.println(DateTimeMaster.getCurrentDateTime());

}
}

Advertisements

Posted on October 25, 2011, in Class. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: