Compare two Java Date
objects
import java.util.Date;
public class
CompareDateUsingAfterExample
{
public static
void main(String[] args)
{
Date d1 = new Date();
try
{
Thread.sleep(10);
}
catch(Exception e)
{
}
Date d2 = new Date();
System.out.println("First
Date : " + d1);
System.out.println("Second
Date : " + d2);
System.out.println("Is
second date after first ? : " + d2.after(d1));
}
}
Compare two Java Date
objects using before method
import java.util.Date;
public class CompareDateUsingBeforeExample
{
public static void
main(String[] args)
{
Date d1 = new
Date();
try
{
Thread.sleep(10);
}
catch(Exception e)
{
}
Date d2 = new Date();
System.out.println("First
Date : " + d1);
System.out.println("Second
Date : " + d2);
System.out.println("Is
first date before second ? : " + d1.before(d2));
}
}
Compare two Java Date objects using compare To method
import java.util.Date;
public class
CompareDateUsingCompareToExample
{
public static void
main(String[] args)
{
Date d1 = new Date();
try
{
Thread.sleep(10);
}
catch(Exception e)
{
}
Date d2 = new Date();
System.out.println("First
Date : " + d1);
System.out.println("Second
Date : " + d2);
int results =
d1.compareTo(d2);
if(results > 0)
System.out.println("First
Date is after second");
else if (results <
0)
System.out.println("First
Date is before second");
else
System.out.println("Both
dates are equal");
}
}
Convert Date into
milliseconds
import java.util.*;
public class
ConvertDateIntoMilliSecondsExample
{
public static void
main(String args[])
{
Date date = new
Date();
System.out.println("Date
is : " + date);
System.out.println("Milliseconds
since January 1, 1970, 00:00:00 GMT : "
+ date.getTime());
}
}
Create java Date from
specific time
import java.util.Date;
public class
CreateDateFromSpecificTimeExample
{
public static void
main(String[] args)
{
Date d = new Date(365L
* 24L * 60L * 60L * 1000L);
System.out.println(d);
}
}
Simple Java Date
import java.util.*;
public class
JavaDateExample
{
public static
void main(String args[])
{
Date date = new Date();
System.out.println("Today
is " + date);
}
}
Add AM PM to time using
Simple Date Format
import
java.text.SimpleDateFormat;
import java.util.Date;
public class
AddAMPMToFormattedDate
{
public static void
main(String[] args)
{
Date date = new
Date();
String strDateFormat =
"HH:mm:ss a";
SimpleDateFormat sdf =
new SimpleDateFormat(strDateFormat);
System.out.println("Time
with AM/PM field : " + sdf.format(date));
}
}
Convert date string from
one format to another format using SimpleDateFormat
import java.util.Date;
import
java.text.ParseException;
import
java.text.SimpleDateFormat;
public class
ConvertDateFormats
{
public static void
main(String[] args)
{
String strDate = "12/12/07";
try
{
SimpleDateFormat
sdfSource = new SimpleDateFormat("dd/MM/yy"); Date date =
sdfSource.parse(strDate); SimpleDateFormat sdfDestination = new
SimpleDateFormat("MM-dd-yyyy hh:mm:ss"); strDate =
sdfDestination.format(date);
System.out.println("Date
is converted from dd/MM/yy format to MM-dd-yyyy hh:mm:ss");
System.out.println("Converted
date is : " + strDate);
}
catch(ParseException
pe)
{
System.out.println("Parse
Exception : " + pe);
}
}
}
No comments:
Post a Comment