How to get the name of the day from java sql.Timestamp
object such as Monday, Tuesday?
Asked
Active
Viewed 1.4k times
6
-
1Check out the JodaTime library. It makes date processing at least ten times simpler than java.util.Date and java.util.Calendar. – Jim Ferrans Nov 08 '10 at 23:18
3 Answers
6
If ts
is your Timestamp
object then to get the month in string format:
String month = (new SimpleDateFormat("MMMM")).format(ts.getTime()); // "April"
and for the day of the week:
String day = (new SimpleDateFormat("EEEE")).format(ts.getTime()); // "Tuesday"

Rocky Inde
- 1,511
- 1
- 20
- 27
-
dont use this! cos SimpleDateFormat didn't measure timezone (https://stackoverflow.com/questions/18122608/simpledateformat-parse-loses-timezone/18124407#18124407) but when you setting time from long, you don't know it timezone! – Andriy Antonov Dec 21 '17 at 11:05
-
Any time represented in long is represented as number of milliseconds since epoch. This is UTC. – Rocky Inde Feb 26 '18 at 06:24
5
You convert your java.sql.Timestamp to a java.sql.Date and send it through a Calendar.
java.sql.Timestamp ts = rs.getTimestamp(1);
java.util.GregorianCalendar cal = Calendar.getInstance();
cal.setTime(ts);
System.out.println(cal.get(java.util.Calendar.DAY_OF_WEEK));

bwawok
- 14,898
- 7
- 32
- 43
-
1I think `Calendar.get()` returns an integer. So, this would mean you still have to create a representing string for the day of the week. – Martijn Courteaux Nov 08 '10 at 22:54
-
3There's absolutely no need to construct a new date on it. The `Timestamp` is a **subclass** of `java.util.Date`. So `cal.setTime(ts)` would have worked as good. Further, the recommended practice is to obtain the calendar by its abstract factory as `Calendar cal = Calendar.getInstance()`. – BalusC Nov 09 '10 at 03:13
-
2But it still does not show a string representation of the name of a day like `"Monday"`,`"Montag"`,`"Lundi"` etc. ;-) – Michael Konietzka Nov 09 '10 at 15:17
2
SimpleDateFormat will provide a Locale specific representation using the pattern "EEEE"
:
public static final String getDayName(final java.util.Date date, final java.util.Locale locale)
{
SimpleDateFormat df=new SimpleDateFormat("EEEE",locale);
return df.format(date);
}
Example usage:
System.out.println(getDayName(new Date(),Locale.US));
returns Tuesday
.
But beware that new SimpleDateFormat(..)
is expensive.

Michael Konietzka
- 5,419
- 2
- 28
- 29
-
dont use this! cos SimpleDateFormat didn't measure timezone (https://stackoverflow.com/questions/18122608/simpledateformat-parse-loses-timezone/18124407#18124407) but when you setting time from long, you don't know it timezone! – Andriy Antonov Dec 21 '17 at 11:05