1

I am trying to find out the day number like it is 1 or 2 or 3 by passing a day name like Monday or Tuesday because the day number may change based on systems calendar (some calendars Sunday will the starting day in some Monday).

I need to find out dates of Monday, Friday or Sunday by passing Index of those days and week number.

I am able to find out week number based on Date but to pass day number (or index) I need to find out exact index based on that local System.

Please tell me whether it is possible or not ?

import java.time.LocalDateTime;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
public class HelloWorld{

     public static void main(String []args){

         String str = "2018-06-08 12:34:21";
         int sDate, eDate;
         DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); 
         LocalDateTime assignedDateTime = LocalDateTime.parse(str, formatter);
         //LocalDate assignedDate = assignedDateTime.toLocalDate();
          Instant instant = assignedDateTime.atZone(ZoneId.systemDefault()).toInstant();
          Date dateFromOld = Date.from(instant);

         Calendar calendar = new GregorianCalendar();
         calendar.setTime(dateFromOld);  

        int weekNumber = calendar.get(calendar.WEEK_OF_MONTH);
         int dayofWeek = calendar.get(Calendar.DAY_OF_WEEK);
        System.out.println(assignedDateTime);
        System.out.println(dateFromOld);
        System.out.println(weekNumber);
        System.out.println(dayofWeek);
     }
}

This is the code I wrote and from this I am able to find out week number day number for required Date.

But, I want to find out day number by passing day name and I did't find any methods for this. There are some logic's to find out that like writing Enum functions but this logic also fails in some cases like if system has different staring day from our enum function.

I hope it's clear now.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    Sounds like you have some code already that does something, but there is none in your question. Can you post some code to show the context of your question? – khelwood Jun 08 '18 at 12:03
  • Look into the [java.util.Calendar](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html) class – John Bollinger Jun 08 '18 at 12:04
  • Possible duplicate of [How to determine day of week by passing specific date?](https://stackoverflow.com/questions/5270272/how-to-determine-day-of-week-by-passing-specific-date) – anothernode Jun 08 '18 at 12:05
  • It is definitely possible – soufrk Jun 08 '18 at 12:06
  • Please provide some guidance for this @soufrk. – Barathi Suwin Jun 08 '18 at 12:37
  • 1
    Since you can use `java.time`, the modern Java date and time API, there is no reason that you should want to use any of the old and outdated classes like `Date` and `Calendar` too. The modern API offers all the functionality you can dream of. So no, @JohnBollinger, I strongly disagree about looking into `java.util.Calendar`. – Ole V.V. Jun 08 '18 at 14:30

2 Answers2

2

My first example is France. France uses the international standard (ISO), where Monday is the 1st day of the week, Tuesday is the 2nd, etc. To find the number, we first need to parse the name of the day of week. Next, the appropriate WeekFields object can give us a field that we can use for querying the day of week about its number in the week.

    DateTimeFormatter dayOfWeekFormatter
            = DateTimeFormatter.ofPattern("EEEE", Locale.ENGLISH);

    Locale loc = Locale.FRANCE;
    WeekFields wf = WeekFields.of(loc);

    String dayName = "Tuesday";
    DayOfWeek day = DayOfWeek.from(dayOfWeekFormatter.parse(dayName));
    int dayNumber = day.get(wf.dayOfWeek());
    System.out.println(dayName + " has number " + dayNumber);

Output:

Tuesday has number 2

This was as expected. The other example is USA. American weeks begin on Sunday, so Tuesday should be the 3rd day of the week. We use US locale:

    Locale loc = Locale.US;

The rest of my code is the same, but now the output is:

Tuesday has number 3

The WeekFields class is sometimes a bit tricky to use, but if you can find out how to use it, it can nearly always give you what you want across international, US and other week schemes.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

The calendar week can starts other than Sunday but constant values for weekdays will remains same (Sunday =1, Monday =2 ...). You can use below code to loop through for all weekdays and compare

    String inputDayName = "Thursday";
    int dayId=0;

    for(int i=0; i<7; i++)
    {
        calendar.add(Calendar.DATE, 1);
        String iteratingDayName = calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault());
        if(iteratingDayName.equalsIgnoreCase(inputDayName))
        {
            dayId=calendar.get(Calendar.DAY_OF_WEEK);
            break;
        }
    }
Sai Prasad
  • 286
  • 1
  • 2
  • 13
  • 1
    The troublesome old `Calendar` class was supplanted years ago by the *java.time* classes. This Answer is poor advice in 2018. – Basil Bourque Jun 09 '18 at 01:02