This article describes the method of obtaining early morning timestamps in Java. Share it for your reference, as follows:
One requirement in the past two days is to query the recommended information matching by the user, including a valid time period, in units of days, such as 0:0:00:00.
There are usually two solutions in Java:
The first one: use Calendar . This is simpler and most common. The code is as follows:
package dateTimeDemo;import java.util.Calendar;public class timeDemo { public static void main(String[] args) { // TODO automatic generated method stub Calendar c = Calendar.getInstance(); c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); Long today=c.getTimeInMillis()/1000; System.out.println("www.VeVB.COM - Timestamp of today"+today); }}The second type: directly operate the timestamp , the code is as follows
package dateTimeDemo;public class timeDemo { public static void main(String[] args) { // TODO automatic generated method stub long now = System.currentTimeMillis() / 1000l; long daySecond = 60 * 60 * 24; long dayTime = now - (now + 8 * 3600) % daySecond; System.out.println("www.VeVB.COM - Today's early morning timestamp"+dayTime); }}Running results:
The second one will be better, especially when you give you a timestamp to take the early morning time.
PS: Here are a few time and date related tools for your reference:
Unix timestamp conversion tool:
http://tools.VeVB.COM/code/unixtime
Online Date/Day Calculator:
http://tools.VeVB.COM/jisuanqi/date_jisuanqi
Online date calculator/phase difference day calculator:
http://tools.VeVB.COM/jisuanqi/datecalc
Online date day difference calculator:
http://tools.VeVB.COM/jisuanqi/onlinedatejsq
For more information about Java related content, please check out the topics of this site: "Summary of Java Date and Time Operation Skills", "Tutorial on Java Data Structure and Algorithm", "Summary of Java Operation DOM Node Skills" and "Summary of Java Cache Operation Skills"
I hope this article will be helpful to everyone's Java programming.