java日期时间各种变换及处理

深藏阁楼爱情的钟 2022-05-25 08:20 295阅读 0赞

本文作者:合肥工业大学 管理学院 钱洋 email:1563178220@qq.com 内容可能有不到之处,欢迎交流。
未经本人,允许禁止转载。

java处理时间

个人在平时写程序的时候,会遇到将字符型转化为时间、或者获取当期时间,昨天的时间,获取当前月,当前日,当前季度等等。为此,本人写了一个时间处理类,希望对大家有帮助。其中,该类中的函数,可能还会在以后遇到问题时,继续扩充。

程序

  1. package util;
  2. import java.text.DateFormat;
  3. import java.text.DecimalFormat;
  4. import java.text.ParseException;
  5. import java.text.SimpleDateFormat;
  6. import java.util.ArrayList;
  7. import java.util.Calendar;
  8. import java.util.Date;
  9. import java.util.List;
  10. /** * @author:合肥工业大学 管理学院 钱洋 * @email:1563178220@qq.com * @ */
  11. public class TimeUtils {
  12. public static void main( String[] args ) throws ParseException{
  13. List<String> monthlist = TimeUtils.YearMonth(2017,2018);
  14. for (int i = 0; i < monthlist.size(); i++) {
  15. System.out.println(monthlist.get(i));
  16. }
  17. String time = getMonth("2002-1-08 14:50:38");
  18. System.out.println(time);
  19. System.out.println(getDay("2002-1-08 14:50:38"));
  20. System.out.println(TimeUtils.parseTime("2016-05-19 19:17","yyyy-MM-dd HH:mm"));
  21. String data=getNowMonth();
  22. System.out.println(data);
  23. }
  24. //获取当前时间
  25. public static String GetNowDate(String formate){
  26. String temp_str="";
  27. Date dt = new Date();
  28. SimpleDateFormat sdf = new SimpleDateFormat(formate);
  29. temp_str=sdf.format(dt);
  30. return temp_str;
  31. }
  32. //获取当前月
  33. public static String getMonth( String time ){
  34. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
  35. Date date = null;
  36. try {
  37. date = sdf.parse(time);
  38. Calendar cal = Calendar.getInstance();
  39. cal.setTime(date);
  40. } catch (ParseException e) {
  41. e.printStackTrace();
  42. }
  43. return sdf.format(date);
  44. }
  45. //获取当前日期
  46. public static String getDay( String time ){
  47. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  48. Date date = null;
  49. try {
  50. date = sdf.parse(time);
  51. Calendar cal = Calendar.getInstance();
  52. cal.setTime(date);
  53. } catch (ParseException e) {
  54. e.printStackTrace();
  55. }
  56. return sdf.format(date);
  57. }
  58. //输入时间,解析成"yyyy-MM-dd HH:mm:ss"格式
  59. public static Date parseTime(String inputTime) throws ParseException{
  60. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  61. Date date = sdf.parse(inputTime);
  62. return date;
  63. }
  64. //将时间转成成字符型
  65. public static String dateToString(Date date, String type) {
  66. DateFormat df = new SimpleDateFormat(type);
  67. return df.format(date);
  68. }
  69. //将输入的时间,转化成指定格式
  70. public static Date parseTime(String inputTime, String timeFormat) throws ParseException{
  71. SimpleDateFormat sdf = new SimpleDateFormat(timeFormat);
  72. Date date = sdf.parse(inputTime);
  73. return date;
  74. }
  75. public static Calendar parseTimeToCal(String inputTime, String timeFormat) throws ParseException{
  76. SimpleDateFormat sdf = new SimpleDateFormat(timeFormat);
  77. Date date = sdf.parse(inputTime);
  78. Calendar calendar = Calendar.getInstance();
  79. calendar.setTime(date);
  80. return calendar;
  81. }
  82. public static int getDaysBetweenCals(Calendar cal1, Calendar cal2) throws ParseException{
  83. return (int) ((cal2.getTimeInMillis()-cal1.getTimeInMillis())/(1000*24*3600));
  84. }
  85. //长整型转化为时间
  86. public static Date parseTime(long inputTime){
  87. // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  88. Date date= new Date(inputTime);
  89. return date;
  90. }
  91. public static String parseTimeString(long inputTime){
  92. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  93. Date date= new Date(inputTime);
  94. return sdf.format(date);
  95. }
  96. public static String parseStringTime(String inputTime){
  97. String date=null;
  98. try {
  99. Date date1 = new SimpleDateFormat("yyyyMMddHHmmss").parse(inputTime);
  100. date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date1);
  101. } catch (ParseException e) {
  102. // TODO Auto-generated catch block
  103. e.printStackTrace();
  104. }
  105. return date;
  106. }
  107. public static List<String> YearMonth(int year) {
  108. List<String> yearmouthlist=new ArrayList<String>();
  109. for (int i = 1; i < 13; i++) {
  110. DecimalFormat dfInt=new DecimalFormat("00");
  111. String sInt = dfInt.format(i);
  112. yearmouthlist.add(year+sInt);
  113. }
  114. return yearmouthlist;
  115. }
  116. //获取从起始年份到目标年份所有的月
  117. public static List<String> YearMonth(int startyear,int finistyear) {
  118. List<String> yearmouthlist=new ArrayList<String>();
  119. for (int i = startyear; i < finistyear+1; i++) {
  120. for (int j = 1; j < 13; j++) {
  121. DecimalFormat dfInt=new DecimalFormat("00");
  122. String sInt = dfInt.format(j);
  123. yearmouthlist.add(i +""+sInt);
  124. }
  125. }
  126. return yearmouthlist;
  127. }
  128. public static List<String> TOAllDay(int year){
  129. List<String> daylist=new ArrayList<String>();
  130. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  131. int m=1;//月份计数
  132. while (m<13)
  133. {
  134. int month=m;
  135. Calendar cal=Calendar.getInstance();//获得当前日期对象
  136. cal.clear();//清除信息
  137. cal.set(Calendar.YEAR,year);
  138. cal.set(Calendar.MONTH,month-1);//1月从0开始
  139. cal.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
  140. System.out.println("##########___" + sdf.format(cal.getTime()));
  141. int count=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
  142. System.out.println("$$$$$$$$$$________" + count);
  143. for (int j=0;j<=(count - 2);)
  144. {
  145. cal.add(Calendar.DAY_OF_MONTH,+1);
  146. j++;
  147. daylist.add(sdf.format(cal.getTime()));
  148. }
  149. m++;
  150. }
  151. return daylist;
  152. }
  153. //获取昨天的日期
  154. public static String getyesterday(){
  155. Calendar cal = Calendar.getInstance();
  156. cal.add(Calendar.DATE, -1);
  157. String yesterday = new SimpleDateFormat( "yyyy-MM-dd ").format(cal.getTime());
  158. return yesterday;
  159. }
  160. //获取当前年份月份
  161. public static String getNowMonth(){
  162. Calendar cal = Calendar.getInstance();
  163. int year = cal.get(Calendar.YEAR);
  164. int month = cal.get(Calendar.MONTH) + 1;
  165. DecimalFormat dfInt=new DecimalFormat("00");
  166. String sInt = dfInt.format(month);
  167. return year+""+sInt;
  168. }
  169. }

发表评论

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

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

相关阅读