Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
Showing posts with label Date. Show all posts
Showing posts with label Date. Show all posts
Tuesday, 16 April 2013
Converting a Date object to a calendar object
Compare Two Date In Java
public static void main( String[] args )
{
try{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = sdf.parse("2009-12-31");
Date date2 = sdf.parse("2010-01-31");
System.out.println(sdf.format(date1));
System.out.println(sdf.format(date2));
if(date1.compareTo(date2)>0){
System.out.println("Date1 is after Date2");
}else if(date1.compareTo(date2)<0){
System.out.println("Date1 is before Date2");
}else if(date1.compareTo(date2)==0){
System.out.println("Date1 is equal to Date2");
}else{
System.out.println("How to get here?");
}
}catch(ParseException ex){
ex.printStackTrace();
}
}
String to Date Conversion in Java
//String to Date Conversion
stringDate = "10-04-2013";
public Date stringToDate(String stringDate)
{
Date date = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
date = sdf.parse(stringDate);
System.out.println(date);
return date;
} catch (ParseException e) {
e.printStackTrace();
return date;
}
}
Timestamp to Date Conversion In Java
Use This Import.
import java.sql.Timestamp;
//Timestamp to Date Conversion
public Date timeStampToDate(Timestamp timestamp)
{
Date date = null;
try
{
date = new Date(timestamp.getTime());
return date;
}catch (Exception e) {
e.printStackTrace();
return date;
}
}