Monday, January 30, 2012

Working with Date and Time?

For getting current time ------->

public static String getCurrentTimeStamp() {
   SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");//dd/MM/yyyy
   Date now = new Date();
   String strDate = sdfDate.format(now);
   return strDate;
}


For Comparing time---------->

public static boolean TimeComperator(String time1,String time2) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("H:mm"); 
        Date pastDate=(Date) formatter.parse(time1); 
        Date futureDate=(Date)formatter.parse(time2); 
            if(pastDate.before(futureDate)) 
            {
            return true;
            }
            return false;
    }


For comparing date--------->

 public static boolean dateComparator(String date1, String date2)throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date pastDate = (Date) formatter.parse(date1);
Date futureDate = (Date) formatter.parse(date2);
if (pastDate.before(futureDate)||pastDate.equals(futureDate)) {
return true;
}
return false;
}




For email validation---------->



private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; 

    public static boolean EmailValidator(String email) { 
    pattern = Pattern.compile(EMAIL_PATTERN);
    Matcher matcher = pattern.matcher(email); 
    return matcher.matches();
    } 






No comments:

Post a Comment