Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use time format conversion impleDateFormat and Data API in Java

Shulou Source: shulou.com Published: 2022-06-02 14:41:11 09月17日 Update

This article will explain in detail how to use time format conversion impleDateFormat and Data API in Java. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

1. Create a parameterless Data object

Date d1=new Date (); / / create a parameterless Data object

System.out.println (D1); / / D1 returns the current time

For example, return:

Sun Nov 14 18:33:05 CST 2021

two。 Create a Data object with parameters

Returns the time available from 08:00:00 milliseconds on January 1, 1970 plus the number of milliseconds passed in.

1s equals 1000ms

One day: 86400000ms

Date d2=new Date (86400000)

System.out.println (d2); / / returned Fri Jan 02 08:00:00 CST 1970 Fri Jan / return: 8: 00 on January 2, 1970 and 08:00:00 on January 1, 1970, one day short package com.example.demo16;import java.util.Date;public class Daterw {public static void main (String [] args) {Date d1=new Date (); System.out.println (D1); / / D1 returns the current time Date d2=new Date (86400000); System.out.println (D2) / / returned Fri Jan 02 08:00:00 CST 1970}} 3.Data object time format conversion:

What we want is 2021-11-14 18:42.

No: Fri Jan 02 08:00:00 CST 1970 (this format)

How do I change it?

Import

Import java.text.SimpleDateFormat

Use: SimpleDateFormat object (create formatted object)

SimpleDateFormat we=new SimpleDateFormat ("yyyy-MM-dd HH:MM:ss SSSS"); / / format yyyy-MM-dd HH:MM:ss SSSS

/ / cannot change letters yyyy:year,MM: month, dd: day, HH: hours, MM: minutes, ss: seconds, SSSS: milliseconds, can be set: SSS, three digits

String wer=we.format (D1); / / use format () to pass in time

System.out.println (wer)

Package com.example.demo16;import java.text.SimpleDateFormat;import java.util.Date;import static java.lang.String.format;public class Daterw {public static void main (String [] args) {SimpleDateFormat we=new SimpleDateFormat ("yyyy-MM-dd HH:MM:ss SSSS"); / / HH:24 hourly calculation, hh:12 hourly calculation, Date d1=new Date (); System.out.println (D1); / / D1 returns the current time Date d2=new Date (86400000) System.out.println (D2); / / returned Fri Jan 02 08:00:00 CST 1970String wer=we.format (D1); System.out.println (wer);}}

Effect picture:

You can also:

SimpleDateFormat we=new SimpleDateFormat ("yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy

Return:

November 14, 2021 19 hours: 05 minutes: 20 seconds 370 milliseconds (this format)

4. Return: a specified parameter: return year / month / day / hour

Direct: this way

SimpleDateFormat we=new SimpleDateFormat ("yyyy year")

Will return:

2021

SimpleDateFormat we=new SimpleDateFormat ("HH hour") / /

Returned:

19 hours

5.API: getTime ()

Long err=d1.getTime (); / / calculation: from 1970-1-1, how many milliseconds have passed to the present, this number is very long, so use the long data type

Package com.example.demo16;import java.text.SimpleDateFormat;import java.util.Date;import static java.lang.String.format;public class Daterw {public static void main (String [] args) {/ / SimpleDateFormat we=new SimpleDateFormat ("yyyy-mm-dd hh:mm:ss SS"); / / SimpleDateFormat we=new SimpleDateFormat ("yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy Date d1=new Date (); System.out.println (D1); / / D1 returns the current time Date d2=new Date (86400000); System.out.println (D2); / / returns ri Jan 02 08:00:00 CST 1970String wer=we.format (D1); System.out.println (wer); long err=d1.getTime () / / calculation: how many milliseconds have elapsed since 1970-1-1, this number is very long, so use the long data type System.out.println (err); long qqq=System.currentTimeMillis (); / / get how many milliseconds System.out.println (qqq) has elapsed since 1970-1-1;}}

Effect:

On "Java time format conversion impleDateFormat and Data API how to use" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.

Tags: Hours format time objects parameters articles middle tense effects numbers data more present tense type good practical content letters renderings articles months Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Xiaomi Shulou Technology OPPO Reno Apple