Conversion of various time types in Java

Keywords: Java Apache

Catalog

 

Preface:

(1) Time stamp milliseconds to seconds

(2) Time stamp conversion to time type

(3) Convert common time types to timestamps

(4) Use third-party class library conversion

(5) String to Date

(6) Date to String

(7) Calendar to String

(8) String to Calendar

(9) Date to Calendar

Summary:

Preface:

suffer The blog about the time stamp in Ergo Inspired, I decided I'd also like to sort out the conversion of time types.{Silent King Two, Second Brother of the River}

We often encounter this type of timestamp during development.

For example, we usually use the Timestamp type for creation time, but the data returned to the front end is sometimes inaccurate, directly 158455581. In this case, we need to convert it to a familiar time format.

For example, when enterprises WeChat official documents call their clock-in data, most of the time-class data returns a time stamp, so we need to process the data. Today I will sort out the time-stamp format of the conversion.

Of course, there are many online webpages like the one below on Du Nian, which can convert time stamps and time directly.

 

 

(1) Time stamp milliseconds to seconds

 //1. First, convert milliseconds to seconds
       long timeStamp = System.currentTimeMillis();
       int timeStamp2 = (int) (timeStamp / 1000);
       System.out.println("First way: millisecond time:----"+timeStamp + "---Time in seconds is-----"+timeStamp2);

Run result:

 

(2) Time stamp conversion to time type

 //2. Second, how to convert a timestamp to a time type
       //Note: Time stamps cannot be converted directly to String type. Instead, they should be converted to Date type before being converted to String type.

       int timeStamp3 = 1583456151;
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       String resultDate = sdf.format(new Date(timeStamp3 * 1000L));
       System.out.println("Second way: data of timestamp type is converted to time type----"+resultDate);

Run result:

 

(3) Convert common time types to timestamps

     //3. Third, how do you convert normal time into time stamps?
       String startDate = "2020-03-06 08:55:51";
       SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       int timeStampUnix = (int) (simpleDateFormat.parse(startDate).getTime() / 1000);
       System.out.println("The third way is to convert normal time into time stamps:-----"+ timeStampUnix);

Run result:

 

(4) Use third-party class library conversion

 //4. Use third-party class libraries for conversion
       String resultFormatDate = DateFormatUtils.format(1578179845 * 1000L,"yyyy-MM-dd HH:mm:ss");
       System.out.println("4.Use DateFormatUtils Results after conversion:-------"+resultFormatDate);

Run result:

 

Supplementary section:

(5) String to Date

//Supplementary Classes: -----------------------------------------------------------
       //Supplement 1: String to Date
       SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
       Date date = sdf2.parse("2020-03-06");
       System.out.println("Supplement 1: String turn Date----"+date);

Run result:

 

(6) Date to String

 //Supplement 2. Convert Date to String
       System.out.println("Supplement 2: Date turn String------"+sdf2.format(date));

Run result:

(7) Calendar to String

 //Supplement 3. Convert Calendar to String
       //Get the details of the current time, such as year, month, day, week,date, minute, second, etc.
       Calendar calendar = Calendar.getInstance();
       SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("yyyy-MM-dd");
       String resultCalendarDate = simpleDateFormat3.format(calendar.getTime());
       System.out.println("Supplement 3.take Calendar convert to String:-------"+resultCalendarDate);

Run result:

 

(8) String to Calendar

 //Supplement 4. Convert String to Calendar

       String str="2020-03-06";
       SimpleDateFormat simpleDateFormat4= new SimpleDateFormat("yyyy-MM-dd");
       Date date4 =simpleDateFormat4.parse(str);
       Calendar calendar2 = Calendar.getInstance();
       calendar2.setTime(date);
       System.out.println("Supplement 4.take String convert to Calendar-----"+date4);

Run result:

 

(9) Date to Calendar

 

      //Supplement 5, Convert Date to Calendar
       Calendar calendar5 = Calendar.getInstance();
       calendar5.setTime(new Date());
       System.out.println("Supplement 5,-------"+ calendar5.getTime());

 

Run result:

(10) Calendar to Date

 //Supplement 6, Convert Calendar to Date

       Calendar calendar6 = Calendar.getInstance();
       Date date6 =calendar6.getTime();
       System.out.println("Supplement 6:take Calendar convert to Date -----"+date6);

Run result:

 

Summary:

You are welcome to comment and add, if you think the tidy up is good, can you give the tidy up a compliment, thank you

package com.test.nodefault;

import com.test.model.ShoesModel;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author tanghh
 * @Date 2020/2/25 16:31
 */
public class TestShones {
   public static void main(String[]args) throws ParseException {
       //1. First, convert milliseconds to seconds
       long timeStamp = System.currentTimeMillis();
       int timeStamp2 = (int) (timeStamp / 1000);
       System.out.println("First way: millisecond time:----"+timeStamp + "---Time in seconds is-----"+timeStamp2);
       //2. Second, how to convert a timestamp to a time type
       //Note: Time stamps cannot be converted directly to String type. Instead, they should be converted to Date type before being converted to String type.

       int timeStamp3 = 1583456151;
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       String resultDate = sdf.format(new Date(timeStamp3 * 1000L));
       System.out.println("Second way: data of timestamp type is converted to time type----"+resultDate);

       //3. Third, how do you convert normal time into time stamps?
       String startDate = "2020-03-06 08:55:51";
       SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       int timeStampUnix = (int) (simpleDateFormat.parse(startDate).getTime() / 1000);
       System.out.println("The third way is to convert normal time into time stamps:-----"+ timeStampUnix);

       //4. Use third-party class libraries for conversion
       String resultFormatDate = DateFormatUtils.format(1578179845 * 1000L,"yyyy-MM-dd HH:mm:ss");
       System.out.println("4.Use DateFormatUtils Results after conversion:-------"+resultFormatDate);

      //Supplementary Classes: -----------------------------------------------------------
       //Supplement 1: String to Date
       SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
       Date date = sdf2.parse("2020-03-06");
       System.out.println("Supplement 1: String turn Date----"+date);
       //Supplement 2. Convert Date to String
       System.out.println("Supplement 2: Date turn String------"+sdf2.format(date));

       //Supplement 3. Convert Calendar to String
       //Get the details of the current time, such as year, month, day, week,date, minute, second, etc.
       Calendar calendar = Calendar.getInstance();
       SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("yyyy-MM-dd");
       String resultCalendarDate = simpleDateFormat3.format(calendar.getTime());
       System.out.println("Supplement 3.take Calendar convert to String:-------"+resultCalendarDate);
       //Supplement 4. Convert String to Calendar

       String str="2020-03-06";
       SimpleDateFormat simpleDateFormat4= new SimpleDateFormat("yyyy-MM-dd");
       Date date4 =simpleDateFormat4.parse(str);
       Calendar calendar2 = Calendar.getInstance();
       calendar2.setTime(date);
       System.out.println("Supplement 4.take String convert to Calendar-----"+date4);

      //Supplement 5, Convert Date to Calendar
       Calendar calendar5 = Calendar.getInstance();
       calendar5.setTime(new Date());
       System.out.println("Supplement 5,-------"+ calendar5.getTime());

       //Supplement 6, Convert Calendar to Date

       Calendar calendar6 = Calendar.getInstance();
       Date date6 =calendar6.getTime();
       System.out.println("Supplement 6:take Calendar convert to Date -----"+date6);
   }

}

 

54 original articles published. 49. 20,000 visits+
Private letter follow

Posted by misterph on Thu, 05 Mar 2020 18:10:51 -0800