A problem encountered at work, by the way, record it.
Suppose the value of the obtained time and date is a String type, for example: String time_now = "2018-4-28 17:04:05";
When we need to take the previous date or the later time separately, we need to process:
private String getTime(String end) { String time = null; if(end == null) { return ""; }else { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); try { Date d1 = df.parse(end); time = df.format(d1); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace (); } return time; } }The standard output of getting the current time from Date is:
Date ss= new Date();
Sat Apr 28 17:17:22 CST 2018 Such data
After such formatting, you will get the standard String type of time:
SimpleDateFormat format0 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = format0.format(ss.getTime()); System.out.println("Format 0: " + time); SimpleDateFormat format1 = new SimpleDateFormat("yyyy year MM month dd day HH hours mm minutes ss seconds"); time = format1.format(ss.getTime()); System.out.println("Format 1: " + time);
The output result is: formatted result 0: 2018-04-28 17:19:55 Formatted result 1: April 28, 2018 17:19:55