Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
For example, if it's 1 p.m (13:00) and I need to get the number 46800000 (13 hours from beginning o day) in milliseconds. Could anyone please help?
You can use a Calendar to calculate it. You set the time to the hour 0 and calculate the difference:
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 millis = (System.currentTimeMillis() - c.getTimeInMillis());
Joda-Time
Using the Joda-Time 2.5 library, it is an easy one-liner.
long millisOfDay = DateTime.now( DateTimeZone.forID( "America/Montreal" ) ).getMillisOfDay();
Note how time zone is crucial. If omitted you implicitly rely on the JVM’s current default.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
I have the following method that has been for a very long time in a legacy codebase.
public static Calendar nowAsCalendar(String timeZone){
Calendar FDRCal = GregorianCalendar.getInstance(timeZone);
FDRCal.setTime(now());
Calendar cal = Calendar.getInstance();
cal.set(1, FDRCal.get(1));
cal.set(2, FDRCal.get(2));
cal.set(5, FDRCal.get(5));
cal.set(11, FDRCal.get(11));
cal.set(12, FDRCal.get(12));
cal.set(13, FDRCal.get(13));
cal.set(14, FDRCal.get(14));
}
We are setting a brand new Calendar instance cal that uses the default timezone so I think it should break as we are not setting the timezone in the new Calendar instance. Am I overlooking something?. What is the best way to set the current date with a timezone?
In Calendar.getInstance() the new instance uses your system's default time zone, so failing to set it the time zone won't cause anything to break, unless you require the time zone to be something other than the default.
However, Calendar is an obsolete class with many flaws, and it's really best not to use it any more. The more modern class to represent a date and time with a timezone is java.time.ZonedDateTime.
You can instantiate one of these, using the default time zone and the current date and time, by writing ZonedDateTime.now(). There's also a version where you can pass in a ZoneId for a time zone, if you want a different time zone from the default - that is, you can write something like
ZoneId sydneyTime = ZoneId.of("Australia/Sydney");
ZonedDateTime rightNow = ZonedDateTime.now(sydneyTime);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
This post was edited and submitted for review 11 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
Code
Date dates = new Date(Long.MAX_VALUE);
output -
292278994-08-17 12:42:55
I want to get the largest date today onwards. How to get this using another way.292278994 this year cant save my DB I need only 4 characters for the year.
To get the boundary dates of the current year, you can use the java.time API and set the month/day values to january 1st and december 31st respectively:
LocalDateTime now = LocalDateTime.now();
LocalDateTime startOfYear = now.withMonth(1).withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
LocalDateTime endOfYear = now.withMonth(12).withDayOfMonth(31).withHour(23).withMinute(59).withSecond(59).withNano(999999999);
System.out.println(startOfYear);
System.out.println(endOfYear);
Output:
2022-01-01T00:00
2022-12-31T23:59:59.999999999
Epoch milliseconds are milliseconds counted from the very first moment of 1970 and they are stored as long in Java. That means Long.MAX_VALUE will get you the moment in time that is as far in the future as this representation allows. A moment that is beyond some maximum date of the current year, like some hundred million years beyond…
If you are on Java 8 or higher and try to use an Instant with Long.MAX_VALUE like this:
public static void main(String[] args) {
// use the greates Long as epoch millis and create an Instant from it
Instant instant = Instant.ofEpochMilli(Long.MAX_VALUE);
// represent the Instant as date and time of day in a specific zone
ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, ZoneId.of("UTC"));
// define the output format
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
// and print the
System.out.println(zdt.format(dtf));
}
You will get the following output:
+292278994-08-17 07:12:55
You may want to adjust the ZoneId to the desired one and remove the leading plus from the output…
But basically understand that epoch milliseconds are not years.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
How to get today's date and time NOT DEVICE i.e actual
I have tried this and many but all they give device time
String date = new SimpleDateFormat("dd.MM.yyyy, HH:mm:ss", Locale.getDefault()).format(new Date());
You can get time details from internet and then use it. Just Internet is required.
Use android.net.sntp.SntpClient class.
SntpClient client = new SntpClient();
int timeout = 50000;
if (client.requestTime("time-a.nist.gov", timeout)) {
long time = client.getNtpTime();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(time);
calendar.getTime(); // this should be your date
}
1) Your code will not compile due to applying a Format with hours and minutes to a Date
2) You are using default Locale of the device so it will return the local date time.
3) You need to use a timezoned time and apply the timezone of Greenwitch to get current DateTime without any timezone applied.
Check ZonedDateTime for more informations
If I understood you correctly, you might want to get the timestamp from a NTP (Network Time Protocol) server. And java has already had libraries to support your need.
Check this out. (StackOverFlow) How to make my java app get global time from some online clock
// get current default time
System.out.println( Calendar.getInstance().getTime());
// get different time zone
ZoneId zoneId = ZoneId.of("America/Los_Angeles");
LocalTime localTime=LocalTime.now(zoneId);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
String formattedTime=localTime.format(formatter);
System.out.println("Current time of the day in Los Angeles: " + formattedTime);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
From the java.time library, I am using the static method Duration.between to calculate the time in seconds between two LocalDateTime.
Everything works as expected, except for the case below, where I should see a difference of 60 seconds, instead of 1500.
Here is the code to reproduce the error:
import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDateTime;
class Scratch {
public static void main(String[] args) {
LocalDateTime endDate = LocalDateTime.now().with(DayOfWeek.SATURDAY).withHour(0).withMinute(0);
LocalDateTime startDate = LocalDateTime.now().with(DayOfWeek.SUNDAY).withHour(1).withMinute(0);
System.out.println(Duration.between(endDate,startDate).toMinutes());
}
}
I am sure I am missing something here.
There are 25 hours or 1500 minutes between Saturday 00:00 and Sunday 01:00
Maybe do you want Sunday 00:00 and Sunday 01:00?
You are calculating the duration between Saturday morning at midnight (00:00) (or Friday evening at midnight, whichever way you prefer) and Sunday morning at 01:00.
The difference is 25 hours, or 1500 minutes. The result looks fine.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I want to know the number of hours in a day when the DST (daylight saving time, summer time) is begins and ends.
Am willing to use java.time. Zone ID is Europe/London. The main intention is:
when DST begins in the spring, we will have 23 hours in one day because clocks are turned forward
conversely when DST ends, we will have 25 hours in one day.
I have an epoch value from which I should find the number of hours. How is it possible?
ZoneId zone = ZoneId.of("Europe/London");
long epochMillis = 1_540_700_000_000L;
LocalDate date = Instant.ofEpochMilli(epochMillis)
.atZone(zone)
.toLocalDate();
int hoursInDay = (int) ChronoUnit.HOURS.between(
date.atStartOfDay(zone),
date.plusDays(1).atStartOfDay(zone));
System.out.println(date + " is " + hoursInDay + " hours");
I didn’t choose the milliseconds since the epoch at random. In this case the output is:
2018-10-28 is 25 hours
Transition to standard time will happen on the last Sunday in October, this year on October 28.
The Australia/Lord_Howe time zone uses 30-minute DST transitions, so in that time zone the day would be either 23.5 hours or 24.5 hours, which in the above will be truncated to 23 and 24. But for London you should be OK until those crazy British politicians decide to do someting similar. :-)