获取系统时间工具

获取系统时间或对时间运算:

1: 获取当前系统时间并和字符串类型进行相互转换:

2:日期工具类:

代码:

 

import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; public class DateUtil { public final static String FORMAT ="yyyy-MM-dd HH:mm:ss"; /** * 将Date类型转变为指定格式的字符串 */
    public static String formatString(Date date, String format) { SimpleDateFormat dateFormat = new SimpleDateFormat(format, Locale.CHINA); String nowTime = dateFormat.format(date); return nowTime; } public static String formatString() { SimpleDateFormat dateFormat = new SimpleDateFormat(FORMAT, Locale.CHINA); Date date = new Date(); String nowTime = dateFormat.format(date); return nowTime; } /** * 将Date类型转变为指定格式的字符串 */
    public static String formatString(Date date) { SimpleDateFormat dateFormat = new SimpleDateFormat(DateUtil.FORMAT, Locale.CHINA); String nowTime = dateFormat.format(date); return nowTime; } /** * 字符串转换成日期格式 * * @param date * @return * @throws ParseException */
    public static Date parseDate(String date) throws ParseException { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA); Date nowTime = dateFormat.parse(date); return nowTime; } public static Date parseDate(String date,String format) throws ParseException { SimpleDateFormat dateFormat = new SimpleDateFormat(format, Locale.CHINA); Date nowTime = dateFormat.parse(date); return nowTime; } /** * 日期加减运算 * @param date 对指定日期运算 * @param calendarField 对日期中的那个类型进行运算,好比1表示对年运算, 5表示对天进行运算 * @param amount 正数表示加,负数表示减 * @return
     */
    public static Date add(Date date, int calendarField, int amount) { if (date == null) { throw new IllegalArgumentException("The date must not be null"); } Calendar c = Calendar.getInstance(); c.setTime(date); c.add(calendarField, amount); return c.getTime(); }
View Code

 

 

 

3:日期的加减运算:

例子:java

 

 4:mapper中的日期运用:

 


 

 

 

5:当前时间的时间戳转换为字符串类型的时间:app

//long timeStamp = 1495777335060;//直接是时间戳 long timeStamp = System.currentTimeMillis(); //获取当前时间戳,也能够是你自已给的一个随机的或是别人给你的时间戳(必定是long型的数据) SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//这个是你要转成后的时间的格式 String sd = sdf.format(new Date(timeStamp)); // 时间戳转换成时间 System.out.println(sd);//打印出你要的时间  结果就是: 2017-05-26 13:42:15

 

6:获取最近的一个周六:ide

/** * @Author * @Description //TODO 获取当前时间的最近的下一个周六 * @Date 2018/11/29 16:20 * @Param * @return 
     */
    public String getCurrentSturday(){ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CANADA); String format = simpleDateFormat.format(new Date()); try { Date bdate = simpleDateFormat.parse(format); Calendar cal = Calendar.getInstance(); cal.setTime(bdate); if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY){ return format; }else { LocalDate localDate = LocalDate.parse(format); LocalDate nextSaturday = localDate.with(TemporalAdjusters.next(DayOfWeek.SATURDAY)); ZoneId zone = ZoneId.systemDefault(); Instant instant = nextSaturday.atStartOfDay().atZone(zone).toInstant(); Date date = Date.from(instant); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CANADA); String Saturday = dateFormat.format(date); return Saturday; } } catch (ParseException e) { e.printStackTrace(); } return null; }
相关文章
相关标签/搜索