社区
Java SE
帖子详情
如何从一个timestamp得到具体的年月日
yangzhenhai
2003-03-06 02:27:28
如何从一个timestamp得到具体的年月日
...全文
1285
3
打赏
收藏
如何从一个timestamp得到具体的年月日
如何从一个timestamp得到具体的年月日
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
3 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
shine333
2003-03-06
打赏
举报
回复
日不是getDay();是getDate();getDay()返回星期几
charbee
2003-03-06
打赏
举报
回复
timestamp有个方法叫做(long)getTime(),然后把这个long放到java.uitl.Date(),那么你之后想怎么样都行了呀。
java.sql.Timestamp ts = java.sql.Result.getTimestamp();
java.util.Date date =java.util.Date(ts.getTime());
年--> date.getYear();
月--> date.getMonth();
日--> date.getDay();
如果不喜欢Deprecated.
你可以把 java.util.Date 换成 java.util.Calendar 这样就OK了
xiaofenguser
2003-03-06
打赏
举报
回复
getYear();
getMonth();
getDay();
JAVA时间处理类
import java.sql.
Timestamp
; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import org.apache.commons.lang.StringUtils; /** * 日付に関するユーティリティクラス * */ public class DateUtil { /** *
年月日
時のフォーマット(
年月日
) */ public static final String YYYYMMDD = "yyyyMMdd"; /** *
年月日
時のスラッシュ入りフォーマット(年/月/日) */ public static final String YYYYMMDD_SLASH = "yyyy/MM/dd"; /** * 年月のスラッシュ入りフォーマット(年/月) */ public static final String YYYYMM_SLASH = "yyyy/MM"; /** *
年月日
時の中間線入りフォーマット(年-月-日) */ public static final String YYYYMMDD_MID_LINE = "yyyy-MM-dd"; /** * 年月のスラッシュ入りフォーマット(年-月) */ public static final String YYYYMM_MID_LINE = "yyyy-MM"; /** *
年月日
時分秒ミリ秒のフォーマット(
年月日
時分秒ミリ秒) */ public static final String YYYYMMDDHHMMSSSSS = "yyyyMMddHHmmssSSS"; /** *
年月日
時分秒ミリ秒のフォーマット(年-月-日 時:分:秒) */ public static final String YYYYMMDDHHMMSS_MID_LINE = "yyyy-MM-dd HH:mm:ss"; /** *
年月日
時分秒ミリ秒のフォーマット(年/月/日 時:分:秒) */ public static final String YYYYMMDDHHMMSS_SLASH = "yyyy/MM/dd HH:mm:ss"; /** * コンストラクタ */ private DateUtil() { // 何もしない } /** * システム日時を取得する * * @return システム日時 */ public static Date getSystemDate() { return new Date(); } /** * システム日時を
Timestamp
オブジェクトで返却する * * @return
Timestamp
オブジェクト */ public static
Timestamp
get
Timestamp
() { return to
Timestamp
(getSystemDate()); } /** * 指定するフォーマットのシステム日時を取得する * (2008年3月22日15時30分 → 2008-03-22 (format = "yyyy-MM-dd")) * @param format * 指定するフォーマット * @return システム日時 */ public static String getCurrentTime(String format) { return formatDateToStr(getSystemDate(), format); } /** * 現在の月を取得する * * @return 現在の月 */ public static String getCurentMonth() { SimpleDateFormat format = new SimpleDateFormat("MM"); return format.format(getSystemDate()); } /** * Nヶ月後の当月の最初の日を取得する * * @param n * Nヶ月後 * @return Nヶ月後の当月の最初の日 */ public static Calendar getMonthFirstDay(int n) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.DAY_OF_MONTH, 1); cal.add(Calendar.MONTH, n); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); return cal; } /** * Nヶ月後の当月の最後の日を取得する * * @param n * Nヶ月後 * @return Nヶ月後の当月の最後の日 */ public static Calendar getMonthlastDay(int n) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.DAY_OF_MONTH, 1); cal.add(Calendar.MONTH, n + 1); cal.add(Calendar.DATE, -1); cal.set(Calendar.HOUR_OF_DAY, 23); cal.set(Calendar.MINUTE, 59); cal.set(Calendar.SECOND, 59); return cal; } /** * 获得与某日期相隔几天的日期 * * @param date * 指定する日付 * @param addCount * 離れた日数 * @return 処理後の日付 */ public static Date addDay(Date date, int addCount) { if (date == null) { return null; } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DATE, addCount); return calendar.getTime(); } /** * 获得与某日期相隔几天的日期 * * @param date * 指定する日付 * @param addCount * 離れた月数 * @return 処理後の日付 */ public static Date addMonth(Date date, int addCount) { if (date == null) { return null; } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.MONTH, addCount); return calendar.getTime(); } /** * Dateオブジェクトを
Timestamp
オブジェクトに変換する。 * * @param date * Dateオブジェクト * @return
Timestamp
オブジェクト */ public static
Timestamp
to
Timestamp
(Date date) { if (date == null) { return null; } return new
Timestamp
(date.getTime()); } /** * Dateオブジェクトを指定の形式でフォーマットする。 * 例:'yyyy年M月d日 h時m分' * @param date * Dateオブジェクト * @param dateFormat * Dateフォーマット * @return フォーマットされた文字列 */ public static String formatDateToStr(Date date, String dateFormat) { if (date == null) { return ""; } return getFormatter(dateFormat).format(date); } /** * 将某个日期格式字符串转换成另一指定格式日期字符串 例:传入"2009/1/15 16:58:00","yyyy/MM/dd * hh:mm:ss","yyyy年M月d日 a h时m分s秒" 则返回 “2009年1月15日 下午 4时58分0秒” * * @param dateStr * 指定する日付 * @param inDateFormat * 传入日期字符串的日期格式 * @param outDateFormat * 传出日期字符串的日期格式 * @return 返回指定格式的日期字符串 */ public static String formatDateToStr(String dateStr, String inDateFormat, String outDateFormat) { SimpleDateFormat simpleDateFormat = null; String str = ""; Date date = null; try { simpleDateFormat = getFormatter(inDateFormat); date = simpleDateFormat.parse(dateStr); simpleDateFormat.applyPattern(outDateFormat); return simpleDateFormat.format(date); } catch (Exception e) { return str; } } /** * 将字符串转化为格式为formmat 的日期格式返回 * * @param date * 待转换的日期 * @param formmat * 转换的format * @return 转换后的日期 */ public static Date stringToFormatDate(String date, String formmat) { try{ return getFormatter(formmat).parse(date); }catch(Exception e){ return null; } } /** * 日付の正確性チェック * * @param * dateStr 日期パラメータ * @param * format 日期フォーマット * @return 正しい:true 違い:false */ public static boolean isValidDate (String dateStr, String format) { try { getFormatter(format).parse(dateStr); return true; } catch (Exception e) { return false; } } /** * タイムの有効性チェック * * @param dateStr * タイム * @return 有効:true 無効:false */ public static boolean isValidTime (String dateStr) { boolean reval = false; if (!StringUtils.isBlank(dateStr)) { if (dateStr.length() == 4) { try { Integer.parseInt(dateStr); int hour = Integer.parseInt(dateStr.substring(0, 2)); int min = Integer.parseInt(dateStr.substring(2, 4)); if (hour <= 23 && hour >= 0 && min <= 59 && min >= 0) { return true; } } catch (Exception e) { } } } return reval; } /** * 根据输入的日期计算年龄 * * @param birthday 出生日期 格式:yyyy-MM-dd * @return String 年龄(周岁年龄) * */ public static String getAge(String birthday){ if (StringUtils.isBlank(birthday)) { return "0"; } Calendar birthdays = Calendar.getInstance(); try { birthdays.setTime(getFormatter(YYYYMMDD_MID_LINE).parse(birthday)); } catch (ParseException e) { return "0"; } Calendar today = new GregorianCalendar(); int age = today.get(Calendar.YEAR) - birthdays.get(Calendar.YEAR); birthdays.add(Calendar.YEAR, age); if (today.before(birthdays)) { age--; } return String.valueOf(age); } /** * 二つの日付の差を計算する *@param date1 計算日付1 *@param date2 計算日付2 * @return int 日付の差 */ public static int getDateDiffDays(Date date1 ,Date date2) { int retValue = 0; if (date1 != null && date2 != null) { long dateDiff = date1.getTime() - date2.getTime(); retValue = (int)(dateDiff/(24 * 60 * 60 * 1000)); } return retValue; } /** *
得到
具体
某个月的天数 * @throws ParseException */ public static int getDaysOfMonth(String year, String month) throws ParseException{ String datestr = year + "/" + month; Calendar cal = Calendar.getInstance(); SimpleDateFormat dateFormat = getFormatter(YYYYMM_SLASH); cal.setTime(dateFormat.parse(datestr)); int daysOfMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH); return daysOfMonth; } /** * 获取
一个
简单的日期格式化对象 * @param format * @return */ private static SimpleDateFormat getFormatter(String format) { return new SimpleDateFormat(format); } }
MySQL数据库8——数据库中函数的应用详解
数据库中内置函数的使用 该篇主要介绍数据库中内置函数的使用,主要有日期函数,字符串函数,数学函数。 (一)日期函数 select current_date();//获得当前日期,
年月日
select current_time();//获得当前时间,时分秒 select current_
timestamp
();//获得当前
年月日
时分秒 select date('2017-10-28');//获得参数日期 select date_add('2017-10-28', interval 10 day);//在参数日期上加指定年/月/日 select date_sub('2017-10-28'
MySQL 获得当前日期时间的函数小结
1.1 获得当前日期+时间(date + time)函数:now() mysql> select now(); +———————+ | now() | +———————+ | 2008-08-08 22:20:46 | +———————+ 除了 now() 函数能获得当前的日期时间外,MySQL 中还有下面的函数: current_
timestamp
() ,current_
timestamp
,localtime() ,localtime ,local
timestamp
— (v4.0.6) ,local
timestamp
() — (v4.0.6) 这些日期时间函数,都等同于 now()。鉴于
java
timestamp
年月日
_如何在java中从
TimeStamp
获取日期
5 个答案:答案 0 :(得分:0)我不确定你真正想要的是什么。你想获得
一个
java.sql.
Timestamp
实例,还是想把时间戳作为带有上述模式的字符串?也许有帮助:ResultSet rs = ...
Timestamp
t = rs.get
Timestamp
(...);SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:...
TimeStamp
获取
年月日
Timestamp
tmp
Timestamp
=
timeStamp
List.get(i); int year = tmp
Timestamp
.getYear()+1900; int month = tmp
Timestamp
.getMonth() + 1; int day = tmp
Timestamp
.getDate(); int hour = tmp
Timestamp
.getHours()
Java SE
62,614
社区成员
307,326
社区内容
发帖
与我相关
我的任务
Java SE
Java 2 Standard Edition
复制链接
扫一扫
分享
社区描述
Java 2 Standard Edition
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章