Sort by day of the week from Monday to Sunday in Java
Define a class
There is no class bundled with Java to represent a day-of-week combined with a time-of-day. The bundled class would require a date.
You will need to define your own such class, perhaps named DayOfWeekWithTime
. That class will have two member fields, a DayOfWeek
object and a LocalTime
object. Search Stack Overflow for many existing posts on these classes.
public class DayOfWeekWithTime {
// Member fields
DayOfWeek dayOfWeek ;
LocalTime localTime ;
// Constructor
public DayOfWeekWithTime ( DayOfWeek dayOfWeek , LocalTime localTime ) {
Objects.requireNonNull( dayOfWeek ) ;
Objects.requireNonNull( localTime ) ;
this.dayOfWeek = dayOfWeek ;
this.localTime = localTime ;
}
}
The DayOfWeek
enum follows the ISO 8601 standard, so the week is defined as Monday-Sunday. This matches your desired sort order. So you can sort a collection of DayOfWeekWithTime
objects by that member field. See Collections.sort( list , comparator )
. Search Stack Overflow for many existing posts on the topic of sorting objects by a particular field.
To instantiate your DayOfWeekWithTime
objects from string inputs as seen in the Question, split on the SPACE character. See String::split
. Search Stack Overflow for many existing posts on the topic of splitting strings.
Then write a routine to lookup a DayOfWeek
enum for each of your 3-letter input values. You could write a if-else-if
series. Or you could write a Map< String , DayOfWeek >
.
For the time-of-day, you can parse using LocalTime.parse
method. If using 24-hour clock with padding zero on single digits, you need not define a formatting pattern as the java.time classes use standard ISO 8601 formats by default when parsing/generating text. Search Stack Overflow for many existing posts on the topic of parsing with the java.time classes.
Do not use SimpleDateFormat
or the related terrible date-time classes that are now legacy, such as Date
or Calendar
. Use only java.time classes for date-time work.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
- Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and Java SE 7
- Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
How to sort days of week by setting the first day of week?
You can get sorted days by
let days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"] //by default first day of week is 0let firstDayOfWeek = 2 //Mon
let sorted = days.map((_, i) => days[(i+firstDayOfWeek)%7]);
console.log(sorted)
SQL - sorting DATE_FORMAT by weekdays from Monday to Sunday
You need a numeric representation of the day to sort and you get it with weekday()
:
order by weekday(cleaningdate)
Order date by day of the week
You can use to_char(<date>, 'd')
for the day of the week:
SELECT last_name, hire_date, TO_CHAR(hire_date, 'DAY') AS Day
FROM employees
ORDER BY TO_CHAR(hire_date, 'D');
There might be some additional manipulation to get the proper first day, because that depends on internationalization settings.
Sort an array contains some dates by days order like sunday, monday etc. in php
You could use usort()
and date('w')
, to sort your array using the "Numeric representation of the day of the week"
$dates = array(
'2018-03-07', //Wednesday
'2018-03-08', //Thursday
'2018-03-09', //Friday
'2018-03-10', //Saturday
'2018-03-11', //Sunday
'2018-03-12', //Monday
'2018-03-13', //Tuesday
);
usort($dates, function($a, $b) {
return date('w',strtotime($a)) - date('w',strtotime($b)) ;
});
print_r($dates);
Outputs :
Array
(
[0] => 2018-03-11
[1] => 2018-03-12
[2] => 2018-03-13
[3] => 2018-03-07
[4] => 2018-03-08
[5] => 2018-03-09
[6] => 2018-03-10
)
date('w')
returns : 0 (for Sunday) through 6 (for Saturday).
Sort Schedule Order by Week Day (e.g Monday, Tuesday, Wednesday...)
Instead of
{Object.keys(this.state.data.operationHours).map(dayOfWeek => (
<div key={dayOfWeek} className="day-of-week">
take an array with the sorted days, like
{['monday', 'tuesday', 'wednesday'].map(dayOfWeek => (
<div key={dayOfWeek} className="day-of-week">
Related Topics
How to Query Database Name in Oracle SQL Developer
Way to Abort Execution of MySQL Scripts (Raising Error Perhaps)
Joining Two Separate Queries in a Postgresql ...Query... (Possible or Not Possible)
Hiveql - How to Find The Column Value Is Numeric or Not Using Any Udf
Varchar(255) V Tinyblob V Tinytext
Concatenate with Null Values in Sql
Oracle SQL "Select Date from Datetime Field "
Restoring a Database from .Bak File on Another Machine
T-Sql Insert into with Left Join
Referencing a Composite Primary Key
How to Display the Date as Mm/Dd/Yyyy Hh:Mm Am/Pm Using SQL Server 2008 R2
How to Update Rows of Two Tables That Have Foreign Key Restrictions
What Is The Purpose of Rowlock on Delete and When Should I Use It
Update Statement with Multiple Where Conditions
Sql - Stdevp or Stdev and How to Use It
Sql-Style Group by Aggregate Functions in Jq (Count, Sum and etc)