Home Java javaTutorial How to format java date

How to format java date

Jun 26, 2017 am 11:18 AM
date format

1) Use the DateFormat class:

1

2

3

public String toString(Date d) {

SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);

return sdf.format(d);

Copy after login

}
2) Use the String.format() method.

1

2

3

4

5

String.format()的用法类似于C语言的printf,C语言转JAVA的同学一定会喜欢这个方式的。

public static String toString(Date d) {

String format = “%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS”;

return String.format(format, d);

}

Copy after login

The format string description is below
"%1$tY" % means escape, and the "1$tY" after it is the format description, which will be replaced at runtime. Will not be output as ordinary characters. Since a parameter

may be formatted multiple times, "1$" means formatting the first parameter, "tY" means formatting the year field in the time, then "%1$tY "Output the year in which the value of

is d, such as 2014, and by analogy: "%1$tm" outputs the month.
For specific formatting instructions, please refer to the javadoc of the java.util.Formatter class.
3) Use Calendar to format your own

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

public static String toString(Date d) {

Calendar c = Calendar.getInstance();

c.setTime(d);

int year = c.get(Calendar.YEAR);

int month = c.get(Calendar.MONTH) + 1;

int dayInMonth = c.get(Calendar.DAY_OF_MONTH);

int hour = c.get(Calendar.HOUR_OF_DAY);

int minute = c.get(Calendar.MINUTE);

int seconds = c.get(Calendar.SECOND);

String ret = String.valueOf(year);

ret += “-”;

ret += month < 10 ? “0″ + month : String.valueOf(month);

ret += “-”;

ret += dayInMonth < 10 ? “0″ + dayInMonth : String.valueOf(dayInMonth);

ret += ” “;

ret += hour < 10 ? “0″ + hour : String.valueOf(hour);

ret += “:”;

ret += minute < 10 ? “0″ + hour : String.valueOf(minute);

ret += “:”;

ret += seconds < 10 ? “0″ + hour : String.valueOf(seconds);

return ret;

}

Copy after login

The code is very simple, but it is a bit like reinventing the wheel.


The above is the detailed content of How to format java date. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to search previous Weibo by date on Weibo_How to search previous Weibo by date on Weibo How to search previous Weibo by date on Weibo_How to search previous Weibo by date on Weibo Mar 30, 2024 pm 07:26 PM

1. First open the mobile web browser, search for the Weibo web version, and click the avatar button in the upper left corner after entering. 2. Then click Settings in the upper right corner. 3. Click the version switching option in settings. 4. Then select the color version option in the version switch. 5. Click Search to enter the search page. 6. After entering the keywords, click Find People. 7. When the search completion interface appears, click Filter. 8. Finally, enter the specific date in the release time column and click Filter.

How to format c drive with dos command How to format c drive with dos command Feb 19, 2024 pm 04:23 PM

DOS command is a command line tool used in Windows operating system, which can be used to perform various system management tasks and operations. One of the common tasks is to format the hard drive, including the C drive. Formatting the C drive is a relatively dangerous operation because it will erase all data on the C drive and reinitialize the file system. Before performing this operation, make sure you have backed up important files and have a clear understanding of the impact that formatting will have on your computer. The following is formatted in the DOS command line

How to remove the date that appears automatically when printing from PPT handouts How to remove the date that appears automatically when printing from PPT handouts Mar 26, 2024 pm 08:16 PM

1. Let me first talk about the method I used at the beginning, maybe everyone is using it too. First, open [View]——]Remarks Template[. 2. A place where you can actually see the date after opening it. 3. Select it first and delete it. 4. After deleting, click [Close Master View]. 5. Open the print preview again and find that the date is still there. 6. In fact, this date was not deleted here. It should be in the [Handout Master]. Look at the picture below. 7. Delete the date after you find it. 8. Now when you open the preview and take a look, the date is no longer there. Note: In fact, this method is also very easy to remember, because the printed handouts are handouts, so you should look for the [Handout Master].

Why can't the D drive be formatted? Why can't the D drive be formatted? Aug 30, 2023 pm 02:39 PM

The reasons why the D drive cannot be formatted include that the drive is being used by other programs or processes, there is a damaged file system on the drive, hard disk failure and permission issues. Detailed introduction: 1. The reason why the D drive cannot be formatted may be because the drive is being used by other programs or processes. In the Windows operating system, if a program is accessing the files or folders on the D drive, the system will not be able to perform the format operation. ;2. The reason why the D drive cannot be formatted may be because there is a damaged file system on the drive. The file system is used by the operating system to organize and manage files and folders on the storage device, etc.

Computer formatting tutorial Computer formatting tutorial Jan 08, 2024 am 08:21 AM

Many times when using a computer, you will encounter too much garbage, but many users still don’t know how to format the computer. It doesn’t matter. Here is a tutorial on computer formatting for you to take a look at. How to format a computer: 1. Right-click "This PC" on the desktop and click "Manage". 2. Click "Storage" in "Computer Management" to open "Disk Management". 3. Select the hard drive you want to clean, right-click and select "Format". 4. Check "Perform Quick Format" and click "OK" to start formatting.

Will formatting a laptop make it faster? Will formatting a laptop make it faster? Feb 12, 2024 pm 11:54 PM

Will formatting a laptop make it faster? If you want to format your Windows laptop but want to know if it will make it faster, this article will help you know the right answer to this question. Will formatting a laptop make it faster? There are many reasons why users format their Windows laptops. But the most common reason is slow performance or speed of your laptop. Formatting a laptop will completely delete all data stored on the C drive or the hard drive partition where Windows operating system is installed. Therefore, every user will think twice before taking this step, especially when it comes to the performance of the laptop. This article will help you understand whether formatting your laptop will speed it up. Formatting your laptop helps

How to display date and seconds in the top bar of Ubuntu 17.10? How to display date and seconds in the top bar of Ubuntu 17.10? Jan 08, 2024 am 10:41 AM

By default, the top bar of Ubuntu 17.10 only has the current time and no date. What should I do if I want to display the date? Let’s take a look at the detailed tutorial below. 1. Open the terminal in the launcher, or press [Ctrl+Alt+T] 2. Enter in the terminal: sudoaptinstallgnome-tweak-tool 3. After the installation is completed, open the tweak tool 4. Click TopBar 5. Date is the date and seconds is the number of seconds 6. After setting it up, the date and seconds will be displayed on the time in the top bar.

Revealed secrets of cell phone format recovery methods (mobile phone malfunction? Don't worry) Revealed secrets of cell phone format recovery methods (mobile phone malfunction? Don't worry) May 04, 2024 pm 06:01 PM

Nowadays, we will inevitably encounter some problems such as being unable to turn on the phone or lagging, such as system crash, but during use, mobile phones have become an indispensable part of our lives. We are often at a loss, and sometimes, there are no solutions to these problems. To help you solve cell phone problems, this article will introduce you to some methods of cell phone format recovery and restore your phone to normal operation. Back up data - protect important information, such as photos and contacts, from being lost during the formatting process. Before formatting your phone, the first thing to consider is to back up important data and files on your phone. To ensure data security, or choose to transfer files to a cloud storage service, you can back it up by connecting to a computer. Use the system's built-in recovery function - simple

See all articles