By using this code :
SimpleDateFormat format = new SimpleDateFormat(\"yyyy-MM-dd HH:mm:ss\");
Date date = format.parse(dtStart);
return date;
If your date and time were meant for UTC:
LocalDateTime // Represent a date and time-of-day lacking the context of a time zone or offset-from-UTC. Does *NOT* represent a moment.
.parse( // Convert from text to a date-time object.
"2013-02-17 07:00:00"
.replace( " " , "T" ) // Comply with standard ISO 8601 format.
) // Returns a `LocalDateTime` object.
.atOffset( // Determining a moment by assign an offset-from-UTC. Do this only if you are certain the date and time-of-day were intended for this offset.
ZoneOffset.UTC // An offset of zero means UTC itself.
) // Returns a `OffsetDateTime` object. Represents a moment.
.getDayOfWeek() // Extract the day-of-week enum object.
.getDisplayName( // Localize, producing text.
TextStyle.FULL , // Specify how long or abbreviated.
Locale.US // Specify language and cultural norms to use in localization.
) // Returns a `String` object.
Sunday
And…
…
.getMonth()
.getDisplayName( TextStyle.FULL , Locale.US )
February
The modern solution uses the java.time classes that years ago supplanted the terrible old date-time classes such as Date
& SimpleDateFormat
.
Your code ignores the crucial issue of time zone. When you omit an specific zone or offset-from-UTC, the JVM’s current default time zone is implicitly applied. So your results may vary.
Instead, always specify a time zone or offset explicitly in your code.
LocalDateTime
Your input format of YYYY-MM-DD HH:MM:SS lacks an indicator of time zone or offset-from-UTC.
So we must parse as a LocalDateTime
.
Your input format is close to the standard ISO 8601 format used by default in the LocalDateTime
class. Just replace the SPACE in the middle with a T
.
String input = "2013-02-17 07:00:00".replace( " " , "T" ) ;
LocalDateTime ldt = LocalDateTime.parse( input ) ;
ldt.toString(): 2013-02-17T07:00
The LocalDateTime
you now have in hand does not represent a moment, is not a point on the timeline. Purposely lacking a time zone or offset means it cannot, by definition, represent a moment. A LocalDateTime
represents potential moments along a range of about 26-27 hours, the range of time zones around the globe.
ZonedDateTime
If you know for certain a time zone intended for that date and time, apply a ZoneId
to get a ZonedDateTime
.
ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = ldt.atZone( z ) ;
With a ZonedDateTime
, you now have a moment.
Get the day-of-week using the DayOfWeek enum.
DayOfWeek dow = zdt.getDayOfWeek() ;
The DayOfWeek::getDisplayName method translates the name of the day into any human language specified by a Locale such as Locale.US or Locale.CANADA_FRENCH.
String output = dow.getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH );
dimanche
Or, in US English.
String output = dow.getDisplayName( TextStyle.FULL , Locale.US );
Sunday
Similar for month, use Month enum.
Month m = zdt.getMonth() ;
String output = m.getDisplayName( TextStyle.FULL , Locale.US );
February
OffsetDateTime
If you know for certain that date and time-of-day in the LocalDateTime
was meant to represent a moment in UTC, use OffsetDateTime
class.
OffsetDateTime odt = ldt.atOffset( ZoneOffset.UTC ) ; // Assign UTC (an offset of zero hours-minutes-seconds).
MonthDay
You may also be interested in the MonthDay
class if you wish to work with a day and a month without a year.
MonthDay md = MonthDay.from( zdt ) ;
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?
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.
to custom days of week you can use this function
public static String getDayFromDateString(String stringDate,String dateTimeFormat)
{
String[] daysArray = new String[] {"saturday","sunday","monday","tuesday","wednesday","thursday","friday"};
String day = "";
int dayOfWeek =0;
//dateTimeFormat = yyyy-MM-dd HH:mm:ss
SimpleDateFormat formatter = new SimpleDateFormat(dateTimeFormat);
Date date;
try {
date = formatter.parse(stringDate);
Calendar c = Calendar.getInstance();
c.setTime(date);
dayOfWeek = c.get(Calendar.DAY_OF_WEEK)-1;
if (dayOfWeek < 0) {
dayOfWeek += 7;
}
day = daysArray[dayOfWeek];
} catch (Exception e) {
e.printStackTrace();
}
return day;
}
dateTimeFormat for example dateTimeFormat = "yyyy-MM-dd HH:mm:ss";
public static String getDayFromStringDate(String stringDate, String dateFormat, boolean abbreviated) throws ParseException {
String pattern;
if (abbreviated) {
pattern = "E"; // For short day eg: Mon,Tue
} else {
pattern = "EEEE"; // For compete day eg: Monday, Tuesday
}
return new SimpleDateFormat(pattern)
.format(new SimpleDateFormat(dateFormat).parse(stringDate));
}
Eg: if stringDate :- 16/12/2018 then dateFormat : - dd/MM/yyyy
In Kotlin you can also get Current Day Name using this. (Requires API level 26)
(Calendar.getInstance() as GregorianCalendar).toZonedDateTime().dayOfWeek