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.