0

private void emailKeyReleased(java.awt.event.KeyEvent evt) {
Boolean at=false; Boolean dotcom=false;

    for(int x=1; x<=email.getText().length();x++){
    if(email.getText().substring(x-1,x).equals("@") && x>3){
        at=true;
      }
    else{
         elabel.setForeground(Color.red);
         elabel.setText("Bad Email");
    }
   
    if(x>5 && email.getText().substring(x-4,x).equals(".com") && x>3){
   dotcom=true;
      }
    else{
         elabel.setForeground(Color.red);
         elabel.setText("Bad Email");
    }
    if(at==true && dotcom==true){
        elabel.setForeground(Color.blue);
        elabel.setText("Good Email");
    }
    }

i want my code to show message dialog at(mm/dd/yy) label an "incorrect date format or "not 18" and "18+"

1 Answers1

0

I created a function that's chek years by date of bearth (with Calendar class). When you run the main you should enter your date of berth and the computer will print if you're older or younger than 18.

import java.util.Calendar;
import java.util.Date;

import javax.swing.JOptionPane;

public class Main{
    public static void main(String[] args) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, Integer.parseInt(JOptionPane.showInputDialog("Enter Year:")));
        c.set(Calendar.MONTH, Integer.parseInt(JOptionPane.showInputDialog("Enter Month:")) - 1);
        c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(JOptionPane.showInputDialog("Enter Day:")));
        JOptionPane.showMessageDialog(null, 
                "Your date of bearth is " + 
        c.get(Calendar.DAY_OF_MONTH) + "\\" + (c.get(Calendar.MONTH) + 1) + "\\" + c.get(Calendar.YEAR));
        if (validate(c, 18)) {
            System.out.println("you\'re older then 18");
        }else {
            System.out.println("you\'re younger then 18");
        }
    }
    public static boolean validate(Calendar bearth, int yearsDiff) {
        Calendar today = Calendar.getInstance();
        if (today.get(Calendar.YEAR) - yearsDiff > bearth.get(Calendar.YEAR)) {
            return true;
        }
        if (today.get(Calendar.YEAR) - yearsDiff < bearth.get(Calendar.YEAR)) {
            return false;
        }
        if (today.get(Calendar.DAY_OF_YEAR) >= bearth.get(Calendar.DAY_OF_YEAR)) {
            return true;
        }
        return false;
    }
}
Programmer
  • 803
  • 1
  • 6
  • 13
  • 1
    I recommend you don’t use `Calendar`. That class is poorly designed and long outdated. Instead use `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). May also use `DateTimeFormatter` and `ChronoUnit` from the same API. – Ole V.V. Jan 19 '21 at 09:33