javapandit.net
  • Home
  • Quick Java
    • Exception Handling
    • Collections Framework
  • Java Best Practices
  • Web Services
    • Web Service Basics
    • Ten Basic webservice concepts
    • XML
    • Apache Axis
    • Restful Web Services
  • JMS Concepts
    • JMS- MySQL
  • Hadoop
    • NoSQL DATABASEs
    • Apache Sqoop
    • Hadoop Interview Questions
  • Java 5
  • Java 8
    • Java 8 : Lambda Expressions
  • JDBC
  • Java Architect
    • Enterprise application re-platforming strategies
    • Java Memory Management
  • Java Programs
  • Technical Tips
    • How to set JAVA_HOME environment variable
    • How to create an auto increment field in Oracle?
    • Linux Commands
  • Best Java Interview Questions
    • Java Interview Questions- YouTube
  • Interview Questions
    • Java Tech interview Questions
    • Core Java Interview Questions >
      • core tech questions1
      • Java Collection interview questions
      • Java Concurrency
    • Servlets Interview Questions
    • JSP Interview Questions
    • Java Web Services Interview Questions
    • EJB Interview Questions
    • XML Interview Questions
    • JMS Interview Questions
  • Struts Interview Questions
    • Struts 2 Interview Questions
  • Java EE Architects Interview Questions
    • Java Architect Interview Questions
    • Top 10 reasons for Java Enterprise Application Performance Problems
    • Web Application Scalability Questions for IT Architect
  • JavaPandit's Blog
  • Web Services Interview Questions
  • Servlets And JSP
  • Oracle SOA Interview Questions
    • Open ESB /JBI
    • BPEL Language
  • Log4J
  • Ant
  • Maven
  • JMeter
  • JUnit
  • Apache POI Framework
  • ORCALE SERVICE BUS (OSB) Interview Questions
  • J2EE Patterns
    • Model-View-Controller (MVC)
    • Front Controller
    • DAO
    • Business Delegate
    • Session Facade
    • Service Locator
    • Transfer Object
    • Design Patterns >
      • Creational Patterns >
        • Singleton
      • Behavioural Patterns
      • Structural Patterns
    • Intercepting Filter
  • SQL Interview Questions/Lab
  • Best Wall Papers
    • Devotional Songs
  • Java Community
  • HIBERNATE
  • ORACLE CC&B
    • Oracle CC&B Interview Questions
  • Docker
  • Little Princess
    • Sai Tanvi Naming Ceremony Celebrations
    • Rice Feeding Ceremony
    • Sai Tanvi Gallery
  • APPSC Career Guidance
    • AP History
    • Indian Polity
    • Indian Economy
    • Science & Technology
    • Mental Ability and Reasoning
    • Disaster Management
    • Current Affairs and Events
    • General Sciences >
      • Biology
      • Physics
      • Chemistry
    • Previous Question Papers
  • About Us
  • Contact US

Date Comparison in java

package com.javapandit;

import java.util.Date;

public class CompareDates {

    /**
     * Program to Compare Two Different Dates.
     * @author javapandit.net
     */
    public static void main(String[] args) {

        Date date1 = new Date("18-APR-2013");
        Date date2 = new Date("23-APR-2013");
        if(date1.compareTo(date2) < 0){
            System.out.println("--- date1 smaller than date2");
        }else if(date1.compareTo(date2) > 0){
          System.out.println("---- date1 grater than date2 ---");   
        }else{
            System.out.println("---- date1 equal to date2 ---");
        }
    }
}


Date Comparison in java- Reading values at run time

package com.javapandit;

import java.util.Date;
import java.util.Scanner;

public class CompareDates {

    /**
     * Program to Compare Two Different Dates (Reading values from Console)
     * Enter Date values in specified format (12-JAN-2012)
     * @author javapandit.net
     */
    public static void main(String[] args) {
        //Getting input from Java Console/command prompt
        Scanner inputReader = new Scanner(System.in);
        System.out.println("Enter Date-1 (Format 12-JAN-2012)");
        String strDate1 = inputReader.nextLine();
        System.out.println("Enter Date-2 (Format 12-JAN-2012)");
        String strDate2 = inputReader.nextLine();
       
        if (strDate1.length()>0 && strDate2.length()>0) {
            Date date1 = new Date(strDate1);
            Date date2 = new Date(strDate2);
            if (date1.compareTo(date2) < 0) {
                System.out.println("--- date1 smaller than date2");
            } else if (date1.compareTo(date2) > 0) {
                System.out.println("---- date1 greater than date2 ---");
            } else {
                System.out.println("---- date1 equal to date2 ---");
            }
        }else{
            System.err.println("Please Enter Dates for Comparison");
        }
    }
}


Date Sorting in java- Ascending/Descending order of Dates

package com.javapandit;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class SortingDates {

    /**
     * Program to Sort Dates in Ascending/Descending Order
     *  and Shuffling dates
     *
     * @author javapandit.net
     */
    public static void main(String[] args) {

        if(args.length > 0){
            List<Date> datesList = new ArrayList();
            for(String date : args){
                Date convDate = new Date(date);
                datesList.add(convDate);
            }
            System.out.println(" Before Sorting Dates list -------"+datesList);
            // Sorting list
            //NOTE: Collection.sort() method allows only List values.
            Collections.sort(datesList);
            System.out.println(" After Sorting Dates list --------"+datesList);
            // Reverse List
            Collections.reverse(datesList);
            System.out.println(" After Reversing Dates list -------"+datesList);
            // Shuffle list
            Collections.shuffle(datesList);
            System.out.println(" After Shuffle Dates List -------"+datesList);    
        }
    }
}


Input:
12-JAN-2012
25-DEC-2011
23-FEB-2012
11-MAR-2013
19-JAN-2013
11-JUN-2010

Output:
Before Sorting Dates list -------[Thu Jan 12 00:00:00 GMT+05:30 2012, Sun Dec 25 00:00:00 GMT+05:30 2011, Thu Feb 23 00:00:00 GMT+05:30 2012, Mon Mar 11 00:00:00 GMT+05:30 2013, Sat Jan 19 00:00:00 GMT+05:30 2013, Fri Jun 11 00:00:00 GMT+05:30 2010]

 After Sorting Dates list --------[Fri Jun 11 00:00:00 GMT+05:30 2010, Sun Dec 25 00:00:00 GMT+05:30 2011, Thu Jan 12 00:00:00 GMT+05:30 2012, Thu Feb 23 00:00:00 GMT+05:30 2012, Sat Jan 19 00:00:00 GMT+05:30 2013, Mon Mar 11 00:00:00 GMT+05:30 2013]

After Reversing Dates list -------[Mon Mar 11 00:00:00 GMT+05:30 2013, Sat Jan 19 00:00:00 GMT+05:30 2013, Thu Feb 23 00:00:00 GMT+05:30 2012, Thu Jan 12 00:00:00 GMT+05:30 2012, Sun Dec 25 00:00:00 GMT+05:30 2011, Fri Jun 11 00:00:00 GMT+05:30 2010]

 After Shuffle Dates List -------[Mon Mar 11 00:00:00 GMT+05:30 2013, Fri Jun 11 00:00:00 GMT+05:30 2010, Thu Jan 12 00:00:00 GMT+05:30 2012, Sun Dec 25 00:00:00 GMT+05:30 2011, Sat Jan 19 00:00:00 GMT+05:30 2013, Thu Feb 23 00:00:00 GMT+05:30 2012]

Powered by Create your own unique website with customizable templates.