Reputation: 355
I'm trying to get the time between current time to the target time (later) in milliseconds format.
For example I want to get the time between today that is 29/01/2022 to 10/02/2022 in milliseconds format.
How to do this in Java?
In fact I'm trying to do something after x time and this code I'm using :
SimpleDateFormat hour = new SimpleDateFormat("HH");
SimpleDateFormat minute = new SimpleDateFormat("mm");
Date date = new Date();
Calendar calnow = Calendar.getInstance();
calnow.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour.format(date)));
calnow.set(Calendar.MINUTE,Integer.parseInt(minute.format(date)));
alarmManager.set(AlarmManager.RTC_WAKEUP, calnow.getTimeInMillis() + 10000, this.pendingIntent);
Upvotes: 1
Views: 110
Reputation: 355
I finally found an easy to use solution, I will share it maybe someone need that
This is the full class :
And I accept any other suggestions, or edit.
import android.content.Intent;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
public class Time {
public static int getMilisecondsBetween(String date1, String date2){
//long daysNumber = 0;
long diffInMillies = 0;
try {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH);
Date firstDate = sdf.parse(date1);
Date secondDate = sdf.parse(date2);
diffInMillies = Math.abs(secondDate.getTime() - firstDate.getTime());
// daysNumber = TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS);
} catch (Exception e){
System.out.println(e);
}
return (int) diffInMillies;
}
public static int getDaysNumberBetween(String date1, String date2){
long daysNumber = 0;
long diffInMillies = 0;
try {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH);
Date firstDate = sdf.parse(date1);
Date secondDate = sdf.parse(date2);
diffInMillies = Math.abs(secondDate.getTime() - firstDate.getTime());
daysNumber = TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS);
} catch (Exception e){
System.out.println(e);
}
return (int) daysNumber;
}
public static int daysFromNowTo(String date){
long daysNumber = 0;
long diffInMillies = 0;
try {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
Date c = Calendar.getInstance().getTime();
String currentDate = sdf.format(c);
Date firstDate = sdf.parse(currentDate);
Date secondDate = sdf.parse(date);
diffInMillies = Math.abs(secondDate.getTime() - firstDate.getTime());
daysNumber = TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS);
} catch (Exception e){
System.out.println(e);
}
return (int) daysNumber;
}
public static int milisFromNowTo(String date){
long diffInMillies = 0;
try {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
Date currentDate = Calendar.getInstance().getTime();
Date secondDate = sdf.parse(date);
diffInMillies = Math.abs(secondDate.getTime() - currentDate.getTime());
} catch (Exception e){
System.out.println(e);
}
return (int) diffInMillies;
}
public static int convertToMilis(int time, String cmd){
long milis = 0;
try {
switch (cmd){
case "w" :
milis = time * 604800000;
break;
case "d" :
milis = time * 86400000;
break;
case "h" :
milis = time * 3600000;
break;
case "m" :
milis = time * 60000;
break;
case "s" :
milis = time * 1000;
break;
}
} catch (Exception e){
System.out.println(e);
}
return (int) milis;
}
}
to use it is very easy, example :
to get all milliseconds between two specific dates :
int milis = Time.getMilisecondsBetween("04/02/2022", "10/02/2022");
to get days number between two specific dates :
int daysNumber = Time.getDaysNumberBetween("04/02/2022", "10/02/2022");
to get days number from current date to specific date :
int daysFromToday = Time.daysFromNowTo("10/02/2022");
to get milliseconds count from today to specific date :
int millis = Time.milisFromNowTo("10/02/2022");
convert any time, periode, ...ect to milliseconds :
. w = weeks
. d = days
. h = hours
. m = minutes
. s = seconds
// here 3 days to milliseconds as example
int millis = Time.convertToMilis(3, "d");
Upvotes: 0
Reputation: 338516
Duration
.between(
LocalDate
.of( 2022 , Month.JANUARY , 29 )
.atStartOfDay(
ZoneId.of( "Asia/Tokyo" )
)
,
LocalDate
.of( 2022 , 2 , 10 )
.atStartOfDay(
ZoneId.of( "Asia/Tokyo" )
)
)
.toMillis()
See this code run live at IdeOne.com.
1036800000
Never use the terrible legacy date-time classes such as Date
and Calendar
. Use only java.time classes.
Android 26+ includes an implementation of java.time. For earlier Android, the latest tooling makes most of the functionality available via “API desugaring”.
Define your start and end dates.
LocalDate startDate = LocalDate.of( 2022 , Month.JANUARY , 29 ) ;
LocalDate endDate = LocalDate.of( 2022 , 2 , 10 ) ;
Determine the first moment of the day for each date. This requires a time zone. The day starts at an earlier moment in the east than in the west.
ZoneId z = ZoneId.of( "America/Edmonton" ) ;
ZonedDateTime start = startDate.atStartOfDay( z ) ;
ZonedDateTime end = endDate.atStartOfDay( z ) ;
Calculate elapsed time. Use Duration
class.
Duration d = Duration.between( start , end ) ;
Extract a count of milliseconds. Beware of potential data loss, as Duration
may contain microseconds and nanoseconds.
long millis = d.toMillis() ;
All of this has been covered many times on Stack Overflow. So search to learn more.
Upvotes: 4