一、获取当前时间
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Timestamp newDate = Timestamp.valueOf(df.format(new Date()).toString());
System.out.println("当前时间为"+newDate);
二、获取前或者后一段时间(前一个月、一年等)
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, 0);
Date date = calendar.getTime();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Timestamp beforeDate = Timestamp.valueOf(df.format(date).toString());
/*这里Calendar.MONTH参数为月份,可以是小时、天、周、月、年等,后面的参数为整型,代表是多少月,前一个月则为 -1、后三个月为 3 等,根据需求自行调整。