获取时间

我就是我 2022-07-10 23:46 279阅读 0赞
  1. public class Test(){
  2. public static String FORMAT_SHORT = "yyyy-MM-dd";
  3. public static String FORMAT_LONG = "yyyy-MM-dd HH:mm:ss";
  4. public static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss.S";
  5. public static String FORMAT_SHORT_CN = "yyyy年MM月dd";
  6. public static String FORMAT_LONG_CN = "yyyy年MM月dd日 HH时mm分ss秒";
  7. public static String FORMAT_FULL_CN = "yyyy年MM月dd日 HH时mm分ss秒SSS毫秒";
  8. /** 获取时间戳 **/
  9. public static String getTimeString() {
  10. SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL);
  11. Calendar calendar = Calendar.getInstance();
  12. return df.format(calendar.getTime());
  13. }
  14. /** 获取年份 **/
  15. public static String getYear(Date date) {
  16. return format(date).substring(0, 4);
  17. }
  18. /** 获取月份 **/
  19. public static int getMonth(Date date) {
  20. calendar = Calendar.getInstance();
  21. calendar.setTime(date);
  22. return calendar.get(Calendar.MONTH) + 1;
  23. }
  24. /** 获取日份 **/
  25. public static int getDay(Date date) {
  26. calendar = Calendar.getInstance();
  27. calendar.setTime(date);
  28. return calendar.get(Calendar.DAY_OF_MONTH);
  29. }
  30. /** 获取小时 **/
  31. public static int getHour(Date date) {
  32. calendar = Calendar.getInstance();
  33. calendar.setTime(date);
  34. return calendar.get(Calendar.HOUR_OF_DAY);
  35. }
  36. /** 获取分钟 **/
  37. public static int getMinute(Date date) {
  38. calendar = Calendar.getInstance();
  39. calendar.setTime(date);
  40. return calendar.get(Calendar.MINUTE);
  41. }
  42. /** 获取秒钟 **/
  43. public static int getSecond(Date date) {
  44. calendar = Calendar.getInstance();
  45. calendar.setTime(date);
  46. return calendar.get(Calendar.SECOND);
  47. }
  48. /** 获取毫秒 **/
  49. public static long getMillis(Date date) {
  50. calendar = Calendar.getInstance();
  51. calendar.setTime(date);
  52. return calendar.getTimeInMillis();
  53. }
  54. }

发表评论

表情:
评论列表 (有 0 条评论,279人围观)

还没有评论,来说两句吧...

相关阅读

    相关 java获取时间

    本文章用于记录简单的获取时间,主要是怕自己忘记了。(也方便新手)   //将某个日期以固定(自定义)格式转化成字符串 SimpleDateFormat sdf =