简体   繁体   English

如何从java sql.Timestamp对象获取当天的名字?

[英]How to get the name of the day from java sql.Timestamp object?

如何从周一,周二的java sql.Timestamp对象获取当天的名字?

If ts is your Timestamp object then to get the month in string format: 如果ts是您的Timestamp对象,那么以字符串格式获取月份:

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"

You convert your java.sql.Timestamp to a java.sql.Date and send it through a Calendar. 您将java.sql.Timestamp转换为java.sql.Date并通过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));

SimpleDateFormat will provide a Locale specific representation using the pattern "EEEE" : SimpleDateFormat将使用"EEEE"模式提供Locale特定表示:

 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 . Tuesday回来。

But beware that new SimpleDateFormat(..) is expensive . 但要注意new SimpleDateFormat(..)昂贵的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM