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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4128872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:54:07  来源:igfitidea点击:

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

javatimestamp

提问by fatih

How to get the name of the day from java sql.Timestampobject such as Monday, Tuesday?

如何从java sql.Timestamp星期一,星期二等对象中获取日期的名称?

采纳答案by bwawok

You convert your java.sql.Timestamp to a java.sql.Date and send it through a Calendar.

您将 java.sql.Timestamp 转换为 java.sql.Date 并通过日历发送它。

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

回答by Rocky Inde

If tsis your Timestampobject 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"

回答by Michael Konietzka

SimpleDateFormatwill provide a Localespecific representation using the pattern "EEEE":

SimpleDateFormat将使用模式提供特定于区域设置的表示"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.

返回Tuesday

But beware that new SimpleDateFormat(..)is expensive.

但要注意new SimpleDateFormat(..)昂贵的