How to get day name from java sql.Timestamp object?

How to get day name from java sql.Timestamp object e.g. monday, tuesday?

+6
java timestamp
source share
3 answers

You convert java.sql.Timestamp to java.sql.Date and submit it via 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)); 
+5
source share

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" 
+5
source share

SimpleDateFormat will provide Locale with a specific view using the "EEEE" pattern:

  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); } 

Usage example:

 System.out.println(getDayName(new Date(),Locale.US)); 

returns Tuesday .

But beware that new SimpleDateFormat(..) expensive .

+2
source share

All Articles