java date conversion instance code between various formats

  • 2020-06-12 09:15:36
  • OfStack

java date conversion instance code between various formats

java date converts between various formats, calling static methods directly

Example code:


java A conversion between various date formats that calls static methods directly 


package com.hxhk.cc.util;
 
 
import java.text.SimpleDateFormat;
import java.util.Date;
 
import com.lowagie.text.pdf.codec.postscript.ParseException;
 
public class DateUtil {
 
  /**
   * @param args
   * @throws java.text.ParseException 
   * @throws ParseException 
   */
  public static void main(String[] args) throws ParseException, java.text.ParseException {
    DateUtil du = new DateUtil();
    //String s = du.numToDate(1350144260, "yyyy-MM-dd hh:mm:ss");
    long time = du.stringToLong("2012-10-15 8:44:53", "yyyy-MM-dd hh:mm:ss")/1000;
    long time1 = du.stringToLong("2012-10-15 20:44:53", "yyyy-MM-dd hh:mm:ss")/1000;
    String date = du.longToString(1350470693,"yyyy-MM-dd hh:mm:ss" );
    System.out.println(time);
    System.out.println(time1);
    System.out.println(date);
     
 
 
  }
  // date Type converted to String type 
   // formatType Format for yyyy-MM-dd HH:mm:ss//yyyy years MM month dd day  HH when mm points ss seconds 
   // data Date Type of time 
   public static String dateToString(Date data, String formatType) {
   return new SimpleDateFormat(formatType).format(data);
   }
   
   // long Type converted to String type 
   // currentTime To convert long Type of time 
   // formatType To convert string Type of time format 
   public static String longToString(long currentTime, String formatType)
   throws ParseException, java.text.ParseException {
   Date date = longToDate(currentTime, formatType); // long Type into Date type 
   String strTime = dateToString(date, formatType); // date Type into String
   return strTime;
   }
   
   // string Type converted to date type 
   // strTime To convert string Type of time, formatType The format to convert yyyy-MM-dd HH:mm:ss//yyyy years MM month dd day 
   // HH when mm points ss Second, 
   // strTime The time format must match formatType Time format is the same 
   public static Date stringToDate(String strTime, String formatType)
   throws ParseException, java.text.ParseException {
   SimpleDateFormat formatter = new SimpleDateFormat(formatType);
   Date date = null;
   date = formatter.parse(strTime);
   return date;
   }
   
   // long convert Date type 
   // currentTime To convert long Type of time 
   // formatType The time format to convert yyyy-MM-dd HH:mm:ss//yyyy years MM month dd day  HH when mm points ss seconds 
   public static Date longToDate(long currentTime, String formatType)
   throws ParseException, java.text.ParseException {
   Date dateOld = new Date(currentTime); //  According to the long Type of number of milliseconds of life 1 a date Type of time 
   String sDateTime = dateToString(dateOld, formatType); //  the date Type of time to string
   Date date = stringToDate(sDateTime, formatType); //  the String Type converted to Date type 
   return date;
   }
   
   // string Type converted to long type 
   // strTime To convert String Type of time 
   // formatType Time format 
   // strTime Time format and formatType The time format must be the same 
   public static long stringToLong(String strTime, String formatType)
   throws ParseException, java.text.ParseException {
   Date date = stringToDate(strTime, formatType); // String Type into date type 
   if (date == null) {
   return 0;
   } else {
   long currentTime = dateToLong(date); // date Type into long type 
   return currentTime;
   }
   }
   
   // date Type converted to long type 
   // date To convert date Type of time 
   public static long dateToLong(Date date) {
   return date.getTime();
   }
   public static String numToDate(int number,String formatType){
     Date date = new Date(number);
     SimpleDateFormat sdf = new SimpleDateFormat(formatType);
     return sdf.format(date);
   }
 
}

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: