[JAVA] A memo when you want to clear the time part of the calendar

What is this?

A personal memo when you want to clear the time part of the date and time data acquired by Calendar.

First of all, an example that comes out when you google

calenderObj.clear(Calendar.HOUR_OF_DAY);
calenderObj.clear(Calendar.HOUR);
calenderObj.clear(Calendar.MINUTE);
calenderObj.clear(Calendar.SECOND);
calenderObj.clear(Calendar.MILLISECOND);

What's wrong?

Write a program and check it.


import java.util.Calendar;

public class CalendarClear {

    public static void main(String[] args) {

        Calendar calendar = Calendar.getInstance();
        sysout(calendar);

        calendar.clear(Calendar.HOUR);
        calendar.clear(Calendar.MINUTE);
        calendar.clear(Calendar.SECOND);
        calendar.clear(Calendar.MILLISECOND);
        sysout(calendar);

        calendar.clear(Calendar.HOUR_OF_DAY);
        sysout(calendar);

    }

    private static final void sysout(Calendar calc) {

        System.out.println("/--------------------");

        System.out.println("YEAR:" + calc.get(Calendar.YEAR));
        System.out.println("MONTH:" + calc.get(Calendar.MONTH));
        System.out.println("DATE:" + calc.get(Calendar.DATE));
        System.out.println("HOUR_OF_DAY:" + calc.get(Calendar.HOUR_OF_DAY));
        System.out.println("HOUR:" + calc.get(Calendar.HOUR));
        System.out.println("MINUTE:" + calc.get(Calendar.MINUTE));
        System.out.println("SECOND:" + calc.get(Calendar.SECOND));
        System.out.println("MILLISECOND:" + calc.get(Calendar.MILLISECOND));

    }

}

The result is this.

/--------------------
YEAR:2017
MONTH:4
DATE:22
HOUR_OF_DAY:17
HOUR:5
MINUTE:25
SECOND:35
MILLISECOND:527
/--------------------
YEAR:2017
MONTH:4
DATE:22
HOUR_OF_DAY:17
HOUR:5
MINUTE:0
SECOND:0
MILLISECOND:0
/--------------------
YEAR:2017
MONTH:4
DATE:22
HOUR_OF_DAY:17
HOUR:5
MINUTE:0
SECOND:0
MILLISECOND:0

The values of HOUR_OF_DAY and HOUR are not cleared. This is the correct description.

public static void main(String[] args) {

    Calendar calendar = Calendar.getInstance();
    sysout(calendar);

    calendar.clear(Calendar.MINUTE);
    calendar.clear(Calendar.SECOND);
    calendar.clear(Calendar.MILLISECOND);
    //To clear the time part, you have to put it in set.
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    sysout(calendar);

    calendar.clear();
    sysout(calendar);

}

It is clearly stated in JavaDoc. Calendar (Java Platform SE 7 )

The HOUR_OF_DAY, HOUR, and AM_PM fields are treated separately and the time resolution rule applies. Clearing any of the> fields does not reset the "hours" in this Calendar. Use set (Calendar.HOUR_OF_DAY, 0) to reset the "hour" value.

Processing result.

/--------------------
YEAR:2017
MONTH:4
DATE:22
HOUR_OF_DAY:17
HOUR:5
MINUTE:27
SECOND:12
MILLISECOND:718
/--------------------
YEAR:2017
MONTH:4
DATE:22
HOUR_OF_DAY:0
HOUR:0
MINUTE:0
SECOND:0
MILLISECOND:0

bonus

calendar.clear();

When you do

/--------------------
YEAR:1970
MONTH:0
DATE:1
HOUR_OF_DAY:0
HOUR:0
MINUTE:0
SECOND:0
MILLISECOND:0

All items are cleared.

Calendar (Java Platform SE 7 )

Set all calendar field and time values (offsets in milliseconds from the epoch) for this calendar to undefined.

Recommended Posts

A memo when you want to clear the time part of the calendar
When you want to change the MySQL password of docker-compose
[Swift] When you want to know if the number of characters in a String matches a certain number ...
Set the time of LocalDateTime to a specific time
When you want to use the method outside
When you want to check whether the contents of a property can be converted to a specific type
Defense Techniques When You Have to Fight the Performance of Unfamiliar Applications (Part 2)
A memo to check when you try to use Lombok
[rails] After option useful when you want to change the order of DB columns
A memo to do for the time being when building CentOS 6 series with VirtualBox
I want to expand the clickable part of the link_to method
[java tool] A useful tool when you want to send the ipmsg log of PC-A to the specified PC on a regular basis.
When you want to change the wording to be displayed when making a select box from enum
A memo of the program that allows you to realize that the probability of dice rolling is about 1/6
When you want to ZIP download the image data saved locally
A collection of patterns that you want to be aware of so as not to complicate the code
[Controller] I want to retrieve the numerical value of a specific column from the DB (my memo)
Comparison of version strings (Java implementation) when you want to branch the process between two versions
A memorandum when you want to see the data acquired by Jena & SPARQL for each variable.
[Past POST] I will publish a part of the answer memo when I was a mentor of TECH :: CAMP
A trick when you want to insert a lot of line breaks and tabs when substituting a character string
When you want Rails to disable a session for a specific controller only
I want to know the JSP of the open portlet when developing Liferay
Summary of means when you want to communicate with HTTP on Android
I want you to use Enum # name () for the Key of SharedPreference
When you want to add a string type column with a limited length with the `rails generate migration` command
[RSpec] When you want to use the instance variable of the controller in the test [assigns is not recommended]
An example of a small work when you want to divide the definition value according to the environment but do not want to be aware of it
If you want to satisfy the test coverage of private methods in JUnit
How to create a route directly from the URL you want to specify + α
I want to output the day of the week
Make a margin to the left of the TextField
What I tried when I wanted to get all the fields of a bean
When you want to bind InputStream in JDBI3
I want to var_dump the contents of the intent
A note when you want Tuple in Java
I want you to use Scala as Better Java for the time being
I managed to get a blank when I brought the contents of Beans to the textarea
I want to recursively get the superclass and interface of a certain class
[Ruby] When you want to replace multiple characters
The road to creating a Web service (Part 1)
[JavaScript] I want to limit the processing by checking the input items when the specified time elapses at the time of focus out.
Summary of copy and paste commands used when you want to delete the cache in iOS application development anyway
[Ruby] When you want to assign the result obtained by conditional branching to a variable and put it in the argument
I want you to put the story that the error was solved when you stabbed the charger in the corner of your head
Rspec: I want to test the post-execution state when I set a method on subject
How to delete the tweet associated with the user when you delete it at the same time
If you want to know the options when configuring Ruby, see `RbConfig :: CONFIG ["configure_args "]`
I tried to create a log reproduction script at the time of apt install
A memo about the types of Java O/R mappers and how to select them
[Solution] A memo that I had a hard time because the format of sinatra-validation changed
[Rails] I want to display the link destination of link_to in a separate tab
When the hover of Eclipse is hard to see
I want to call a method of another class
I tried to decorate the simple calendar a little
I want to know the answer of the rock-paper-scissors app
I want to display the name of the poster of the comment
[Java] When writing the source ... A memorandum of understanding ①
Learning memo when learning Java for the first time (personal learning memo)
When you want to dynamically replace Annotation in Java8
If you want to recreate the instance in cloud9