How to find Day at Specified Date in Java

How to find Day at Specified Date in Java

Hello everyone, in this article we are going to talk about finding the name of day at specified Data with Java. There are a lot of approaching to this question In this article we are going to try this one.

Let's get started.

In this example I will use Netbeans and I will create a Java class named as FindDayAtSpecifiedDate. Also I will have a main function which void typed, findDayName function which returns a String and findDifferanceDays method which returns long type data.

Below you can see them :

class FindDayAtSpecifiedDate {

    public static String findDayName(int month, int day, int year) {

    }
    static long findDifferanceDays(String start, String end) 
    { 

    } 
    public static void main(String[] args){

    }

}

Now let's start coding.

Below code block firstly we are going create a date object, with date parameters. Then we are going to get current days name. Then we are going to find the difference between today and target date. We are going to use this value to go to near of around specified date. Then we will use the rest day count to find the target day. We will find the day name within days array.


    public static String findDayName(int month, int day, int year) {
        String output = "";

        //First we are going to create a calendar object and get the todays date from it.
        DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
        Calendar cal = Calendar.getInstance();
        Date date = cal.getTime();
        
        //Then get the todays date as string
        String todaysdate = dateFormat.format(date);
        
        //Find the differance between today and the target date
        long differance = findDifferanceDays(todaysdate, day+"-"+month+"-"+year);

        //find the mod of the differance
        differance = differance % 7;
        //then go to near day to the target date which at same day as today
        while(differance < 0){
            differance +=7;
        }

        //find the day name with rest of day count
        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK) -1;

        String[] days 
            = { 
            "SUNDAY",
            "MONDAY", 
            "TUESDAY", 
            "WEDNESDAY",
            "THURSDAY",
            "FRIDAY",
            "SATURDAY",
            "SUNDAY",
            "MONDAY", 
            "TUESDAY", 
            "WEDNESDAY",
            "THURSDAY",
            "FRIDAY",
            "SATURDAY" };

        if(differance > 0) output = days[dayOfWeek + (int)differance];
        else  output = days[dayOfWeek ];

        //return the day name
        return output;
    }

Below code block will find the time differance between given dates. These dates will be current date and target date. We are going to use this difference to find the target day name in the given days array.

We will find the time from the subtracting. We need to convert this time value to the days. and then return it to the method.


static long findDifferanceDays(String start, String end) 
    { 
        long diff = 0;
        //create the date format
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); 
  
        try { 
            //convert them the date objects
            Date date_start = sdf.parse(start); 
            Date date_end = sdf.parse(end); 
            //find the differance between dates
            long diff_Time = date_end.getTime() - date_start.getTime(); 
            //find the time as days
            diff  = (diff_Time / (1000 * 60 * 60 * 24)) ; 
        } 
        catch (ParseException e) { 
            e.printStackTrace(); 
        } 
        //return it
        return diff;
    } 
Lastly, we are going to call above methods at main function. This will print the name of day.

    public static void main(String[] args){
        //format MM-dd-yyyy
        System.out.println("Day Name :" + findDayName(11, 30, 2019));
        System.out.println("Day Name :" + findDayName(11, 30, 2018));
        System.out.println("Day Name :" + findDayName(11, 30, 2029));
        System.out.println("Day Name :" + findDayName(11, 20, 2119));
        System.out.println("Day Name :" + findDayName(05, 30, 2019));
        System.out.println("Day Name :" + findDayName(06, 11, 2019));
        System.out.println("Day Name :" + findDayName(01, 20, 2019));
    }
You can see the output of this example at below: Example output of Find Day At Specified Date Function

That is all in this article.

Burak Hamdi TUFAN


Tags


Share this Post

Send with Whatsapp

Post a Comment

Success! Your comment sent to post. It will be showed after confirmation.
Error! There was an error sending your comment. Check your inputs!

Comments

  • There is no comment. Be the owner of first comment...