Java Regex




Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!

required
required


Java Date

The class Date epresents a specific instant in time, with millisecond precision.

After Method

Returns true if the invoking Date object contains a date that is later than the one specified by date, otherwise, it returns false.

Date today = DateUtils.addDays(new Date(), 0);
Date yesterday = DateUtils.addDays(new Date(), -1);

System.out.println("today - " + today.toInstant().toString());
System.out.println("yesterday - " + yesterday.toInstant().toString());
System.out.println("today.after(yesterday) - " + today.after(yesterday));
System.out.println("today.equals(yesterday) - " + today.equals(yesterday));
System.out.println("yesterday.after(today) - " + yesterday.after(today));

// Output
today - 2020-10-02T04:34:35.465Z
yesterday - 2020-10-01T04:34:35.480Z
today.after(yesterday) - true
today.equals(yesterday) - false
yesterday.after(today) - false

 

Before Method

Returns true if the invoking Date object contains a date that is earlier than the one specified by date, otherwise, it returns false.

Date today = DateUtils.addDays(new Date(), 0);
Date yesterday = DateUtils.addDays(new Date(), -1);

System.out.println("today - " + today.toInstant().toString());
System.out.println("yesterday - " + yesterday.toInstant().toString());

System.out.println("today.before(yesterday) - " + today.before(yesterday));
System.out.println("today.equals(yesterday) - " + today.equals(yesterday));
System.out.println("yesterday.before(today) - " + yesterday.before(today));

// output
today - 2020-10-02T15:48:24.823Z
yesterday - 2020-10-01T15:48:24.837Z
today.before(yesterday) - false
today.equals(yesterday) - false
yesterday.before(today) - true

 

Check if same two dates are the same day

/**
 * Check if day1 is the same day as day2<br/>
 * day1 - 2020-6-12, day2 - 2020-6-13, return false <br/>
 * day1 - 2020-6-12, day2 - 2020-6-12, return false <br/>
 * day1 - 2020-6-13, day2 - 2020-6-12, return false <br/>
 */
public static boolean isSameDay(Date day1, Date day2) {
    if (day1 == null || day2 == null) {
        return false;
    }
    final Calendar cal1 = Calendar.getInstance();
    cal1.setTime(day1);
    final Calendar cal2 = Calendar.getInstance();
    cal2.setTime(day2);

    return cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) && cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH)
            && cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH);
}

 

Check if day1 is after day2

/**
 * Check if day1 is after day2<br/>
 * day1 - 2020-6-12, day2 - 2020-6-13, return false <br/>
 * day1 - 2020-6-12, day2 - 2020-6-12, return false <br/>
 * day1 - 2020-6-13, day2 - 2020-6-12, return true <br/>
 */
public static boolean isAfterDay(Date day1, Date day2) {
    if (day1 == null || day2 == null) {
        return false;
    }
    final Calendar cal1 = Calendar.getInstance();
    cal1.setTime(day1);
    final Calendar cal2 = Calendar.getInstance();
    cal2.setTime(day2);

    if (cal1.get(Calendar.YEAR) < cal2.get(Calendar.YEAR)) {
        return false;
    } else if (cal1.get(Calendar.MONTH) < cal2.get(Calendar.MONTH)) {
        return false;
    } else if (cal1.get(Calendar.DAY_OF_MONTH) <= cal2.get(Calendar.DAY_OF_MONTH)) {
        return false;
    }

    return true;
}

Check if day1 is before day2

/**
 * Check if day1 is before day2<br/>
 * day1 - 2020-6-12, day2 - 2020-6-13, return true <br/>
 * day1 - 2020-6-12, day2 - 2020-6-12, return false <br/>
 * day1 - 2020-6-13, day2 - 2020-6-12, return false <br/>
 */
public static boolean isBeforeDay(Date day1, Date day2) {
    if (day1 == null || day2 == null) {
        return false;
    }
    final Calendar cal1 = Calendar.getInstance();
    cal1.setTime(day1);
    final Calendar cal2 = Calendar.getInstance();
    cal2.setTime(day2);

    if (cal1.get(Calendar.YEAR) > cal2.get(Calendar.YEAR)) {
        return false;
    } else if (cal1.get(Calendar.MONTH) > cal2.get(Calendar.MONTH)) {
        return false;
    } else if (cal1.get(Calendar.DAY_OF_MONTH) >= cal2.get(Calendar.DAY_OF_MONTH)) {
        return false;
    }

    return true;
}

 

October 2, 2020

Java For Loop

 

For loop is used to execute a statement or a set of statements repeatedly until a certain condition is met.

for(initialization ; condition ; logic for condition){
    // statement(s);
}

First Step: The initialization part of the loop happens first and only executes once.
Second Step: The condition is evaluated on each iteration and if it’s true the statement(s) is executed and if it’s false then the iteration will stop.
Third Step: After every statement execution, the logic for condition is executed which purpose is to make condition meet a limit.

for(int i = 0 ;i < 5 ; i++){
    System.out.println("i is "+i);
}

Output:
i is 0
i is 1
i is 2
i is 3
i is 4

If the condition never becomes false then it’s an infinite loop. Infinite loop is not an expected behavior is Software.

// infinite loop
for(int i = 1 ;i >= 1 ; i++){
    System.out.println("i is "+i);
}
for( ; ; ){
   // statement(s);
}

Using for loop to loop through an array

int[] numbers = {1,2,3,4,5};
for(int i=0;i<numbers.length;i++){
   System.out.println("i is "+numbers[i]);
}

Output:
i is 1
i is 2
i is 3
i is 4
i is 5

Foreach Loop

int[] numbers = {1,2,3,4,5};
for(int i : numbers){
   System.out.println("i is "+i);
}

i is 1
i is 2
i is 3
i is 4
i is 5

 

 

March 12, 2019

Java Conditional Statements

 

Just like the decisions we make in life such as for example buying a car. If the car we are looking for is higher than our searching price then that car is too expensive and we won’t buy it. Else if the car is at our searching price or less we will buy it. In Java and programming in general, we use conditional statements to evaluate our decision.

We use the comparison operators to evaluate our condition.
1. Less than (x < y)
2. Less than or equal to (x <= y)
3. Greater than (x > y)
4. Greater than or equal to (x >= y)
5. Equal (x == y)
6. Not equal (x != y)

If - one if

if (condition) statement;

if (condition) {
  statement;
  statement;
}

 


It is recommended to use {} to specify where the block code ends. It is for readability.

if(10 > 5){
  System.out.println("10 is greater than 5");
}

(10 > 5) – condition
System.out.println(“10 is greater than 5”); – statement

If Else – one if else
if(condition){
        statement;
}else{
        statement;
}

if(5 < 10){
  System.out.println("5 is less than 10");
}else{
  System.out.println("10 is greater than 5");
}
If ElseIf ElseIf Else - multiple if else

if (condition1) {
     //block of code to execute if condition1 is true ;
}else if(condition2){
     //block of code to execute if condition2 is true ;
}else if(condition3){
     //block of code to execute if condition3 is true ;;
}else{
     //block of code to execute if none of the above conditions are true;
}

 

int age 30;
if(age < 19){
  System.out.println("You are a baby.");
} else if (age < 30){
  System.out.println("You are an adult.");
} else {
  System.out.println("You are old.");
}

Switch Statement
Switch statement provides better readability when they are too many if else statements.

String gender = "MALE";
switch(gender){
   case:"MALE";
   System.out.println("You are a man");
   break;
   case:"FEMALE";
   System.out.println("You are a woman");
   break;
   default:
   System.out.println("You are bisexual");
   break;
}

 

IF, IF ELSE, and ELSE

double amount = 0;

if(amount>0) {
    System.out.println("amount>0");
}else if(amount<0) {
    System.out.println("amount<0");
}else {
    System.out.println("amount=0");
}

// output
amount=0
double amount = -0.60;

if(amount < -0.50) {
    System.out.println("amount < -0.50");
}else {
    System.out.println("amount > -0.50");
}

//output
amount < -0.50

 

 

March 12, 2019

Introduction to Java

 

Welcome to my Java tutorial for all levels. Before you dive too deep into my java tutorial I would like to give an idea of what these tutorials are about. What I am teaching here is Java in it’s simplest form so that even a beginner can understand how to program in Java. I share my findings and researches on how to use Java in a practical program or project.

I hope that you will enjoy finding the solution for your bug here! If you are a beginner ride a long with me you never know it might interest you.

 

March 9, 2019

How to solve java problems

March 8, 2019