java implements a method of converting string and date types to each other

  • 2020-06-03 06:27:50
  • OfStack

This article illustrates java's approach to converting strings and date types to and from each other. To share for your reference, specific as follows:


Date inDate = new Date(); // Get current date 
// To establish 1 a 1 Set the format of the  SimpleDateFormat
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String date = f.format(inDate); // will Date Convert to a string 
System.out.println(date); // The output 
Date odate = null;
try { // From a string  Date  Need to be  try/catch
  odate = f.parse(date); // Converts the string to Date
}
catch (ParseException e1) {
  e1.printStackTrace();
}
System.out.println(odate.toString()); // The output 

PS: Here are some more time and date tools for your reference:

Online Date/Day calculator:
http://tools.ofstack.com/jisuanqi/date_jisuanqi

Online date calculator/Difference day calculator:
http://tools.ofstack.com/jisuanqi/datecalc

Online date difference calculator:
http://tools.ofstack.com/jisuanqi/onlinedatejsq

Unix timestamp (timestamp) conversion tool:
http://tools.ofstack.com/code/unixtime

I hope this article has been helpful in java programming.


Related articles: