Como comparar string com date wap

Write a program to read two String variables in DD-MM-YYYY.Compare the two dates and return the older date in ‘MM/DD/YYYY’ format.

Input and Output Format:Input consists of two string.The output consists of a string.

Refer sample output for formatting specifications.

import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; public class Main { /** * @param args * @throws ParseException */ public static void main(String[] args) throws ParseException { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); String s1=sc.nextLine(); String s2=sc.nextLine(); SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-yyyy"); SimpleDateFormat sdf1=new SimpleDateFormat("MM/dd/yyyy"); Date d1=sdf.parse(s1); Date d2=sdf.parse(s2); Calendar cal=Calendar.getInstance(); cal.setTime(d1); long y=cal.getTimeInMillis(); cal.setTime(d2); long y1=cal.getTimeInMillis(); String s3=sdf1.format(d1); String s4=sdf1.format(d2); if(y<y1) System.out.println(s3); else System.out.println(s4); } }

In this tutorial, you will learn –

Let us first understand the parameters that consist of a Date.

Como comparar string com date wap

It will primarily contain –

  • The year (in either 2 or 4 digits)
  • The month (in either 2 digits, First 3 letters of the month or the entire word of the month).
  • The date (it will be the actual date of the month).
  • The day (the day at the given date – like Sun, Mon, Tue, etc.)

Concerning computer systems, there are quite a lot of parameters that can be used to associate with a date. We shall see them in the later parts of this topic.

Display Date in Java

Now let us see how Java provide us the Date. First, we shall see how to get the current date-

Java provides a Date class under the java.util package, The package provides several methods to play around with the date.

You can use the Date object by invoking the constructor of Date class as follows:

import java.util.Date; class Date_Ex1 { public static void main(String args[]) { // Instantiate a Date object by invoking its constructor Date objDate = new Date(); // Display the Date & Time using toString() System.out.println(objDate.toString()); } }

Output:

Wed Nov 29 06:36:22 UTC 2017

In above example date shown in default format, If we want to show the date and time in another format, first understand the Formatting of date.

SimpleDateFormat: Parse and Format Dates

You all must have learned the alphabets in your kindergarten ….

Let us now learn the ABC’s of the date format.

Letter Date or Time Component Examples
G Era designator AD
y Year 2018
M Month in year July or Jul or 07
w Week in year 27
W Week in month 2
D Day in year 189
d Day in month 10
F Day of week in month 2
E Day name in week Tuesday or Tue
u Day number of week (1 = Monday, …, 7 = Sunday) 1
a Am/pm marker PM
H Hour in day (0-23) 0
k Hour in day (1-24) 24
K Hour in am/pm (0-11) 0
h Hour in am/pm (1-12) 12
m Minute in hour 30
s Second in minute 55
S Millisecond 978
z Time zone Pacific Standard Time; PST; GMT-08:00
Z Time zone -0800
X Time zone -08 or -0800 or -08:00

Don’t worry, you don’t need to remember all of these, they can be referred anytime you need to format a particular date.

How to use the SimpleDateFormat?

Java provides a class called a SimpleDateFormat that allows you to format and parse dates in the as per your requirements.

You can use the above characters to specify the format-

For example:

1) Date format required: 2012.10.23 20:20:45 PST

The appropriate date format specified will be- yyyy.MM.dd HH:mm:ss zzz

2) Date format required:09:30:00 AM 23-May-2012

The appropriate date format specified will be-hh:mm:ss a dd-MMM-yyyy

Tip: Be careful with the letter capitalization. If you mistake M with m, you will undesired results!

Let’s learn this with a code example.

import java.text.SimpleDateFormat; import java.util.Date; class TestDates_Format { public static void main(String args[]) { Date objDate = new Date(); // Current System Date and time is assigned to objDate System.out.println(objDate); String strDateFormat = "hh:mm:ss a dd-MMM-yyyy"; //Date format is Specified SimpleDateFormat objSDF = new SimpleDateFormat(strDateFormat); //Date format string is passed as an argument to the Date format object System.out.println(objSDF.format(objDate)); //Date formatting is applied to the current date } }

Output:

Wed Nov 29 06:31:41 UTC 2017 06:31:41 AM 29-Nov-2017

Compare Dates Example

Como comparar string com date wap

The most useful method of comparing dates is by using the method – compareTo()

Let us take a look at the below code snippet-

import java.text.SimpleDateFormat; import java.text.ParseException; import java.util.Date; class TestDates_Compare { public static void main(String args[]) throws ParseException { SimpleDateFormat objSDF = new SimpleDateFormat("dd-mm-yyyy"); Date dt_1 = objSDF.parse("20-08-1981"); Date dt_2 = objSDF.parse("12-10-2012"); System.out.println("Date1 : " + objSDF.format(dt_1)); System.out.println("Date2 : " + objSDF.format(dt_2)); if (dt_1.compareTo(dt_2) > 0) { System.out.println("Date 1 occurs after Date 2"); } // compareTo method returns the value greater than 0 if this Date is after the Date argument. else if (dt_1.compareTo(dt_2) < 0) { System.out.println("Date 1 occurs before Date 2"); } // compareTo method returns the value less than 0 if this Date is before the Date argument; else if (dt_1.compareTo(dt_2) == 0) { System.out.println("Both are same dates"); } // compareTo method returns the value 0 if the argument Date is equal to the second Date; else { System.out.println("You seem to be a time traveller !!"); } } }

Output:

Date1 : 20-08-1981 Date2 : 12-10-2012 Date 1 occurs before Date 2

public class CompareStrings { public static void main(String[] args) { String style = "Bold"; String style2 = "Bold"; if(style == style2) System.out.println("Equal"); else System.out.println("Not Equal"); } }

Output

Equal

In the above program, we've two strings style and style2. We simply use the equal to operator (==) to compare the two strings, which compares the value Bold to Bold and prints Equal.

Example 2: Compare two strings using equals()

public class CompareStrings { public static void main(String[] args) { String style = new String("Bold"); String style2 = new String("Bold"); if(style.equals(style2)) System.out.println("Equal"); else System.out.println("Not Equal"); } }

Output

Equal

In the above program, we have two strings named style and style2 both containing the same world Bold.

However, we've used String constructor to create the strings. To compare these strings in Java, we need to use the equals() method of the string.

You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not.

On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.

If you instead change the program to use equality operator, you'll get Not Equal as shown in the program below.

Example 3: Compare two string objects using == (Doesn't work)

public class CompareStrings { public static void main(String[] args) { String style = new String("Bold"); String style2 = new String("Bold"); if(style == style2) System.out.println("Equal"); else System.out.println("Not Equal"); } }

Output

Not Equal

Example 4: Different ways to compare two strings

Here is the string comparison which is possible in Java.

public class CompareStrings { public static void main(String[] args) { String style = new String("Bold"); String style2 = new String("Bold"); boolean result = style.equals("Bold"); // true System.out.println(result); result = style2 == "Bold"; // false System.out.println(result); result = style == style2; // false System.out.println(result); result = "Bold" == "Bold"; // true System.out.println(result); } }

Output

true false false true