I have a Timestamp being passed from an external source to my application in the 2011-01-23-12.31.45 format. I need to compare it to the current system timestamp an make sure its less than 2 minutes difference. Any ideas on how to accomplish this?
That's a date, not a timestamp. You can parse it using java.text.SimpleDateFormat, using the yyyy-dd-MM-HH.mm.ss format:
SimpleDateFormat sdf = new SimpleDateFormat("yyy-dd-MM-HH.mm.ss");
Date date = sdf.parse(inputDateString);
long timestamp = date.getTime();
And then compare - a minute has 60 * 1000 millis.
Using joda-time for date-time operations is always preferred - it will:
have a thread-safe implementation of the dataformat - DateTimeFormat (the one above is not thread-safe)
simply do Minutes.minutesBetween(..) to find out the minutes between the two instants, rather than calculating.
Well this can be optimized but this is what I came up with. It needs some work but it should get you started.
public class Test {
private final String serverValue = "2011-01-23-12.31.45"; //Old should fail
private final String serverValueNew = "2011-03-28-14.02.00"; //New
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss");
public boolean plusMinusTwoMins(String serverValue) {
boolean withinRange = false;
Date now = Calendar.getInstance().getTime();
Date serverDate = now;
try {
serverDate = dateFormat.parse(serverValue);
} catch (ParseException e) {
e.printStackTrace();
}
long millis = Math.abs(now.getTime() - serverDate.getTime());
System.out.println("Millis: " + millis);
//1000ms * 60s * 2m
if (millis <= (1000 * 60 * 2)) {
withinRange = true;
}
return withinRange;
}
public static void main(String[] args) {
Test test = new Test();
boolean value = test.plusMinusTwoMins(test.serverValue);
System.out.println("Value: " + value);
boolean value2 = test.plusMinusTwoMins(test.serverValueNew);
System.out.println("Value2: " + value2);
}
}
Related
So I'm making an app that gets 2 time strings and subtracts them so I can get the amount of time that passed and for some reason, it always returns 0 please help me.
Here is the code I'm using
static String TimeIn() {
SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss");
Date time = Calendar.getInstance().getTime();
a = format.format(time);
return a;
}
static String TimeOut() {
SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss");
Date time = Calendar.getInstance().getTime();
b = format.format(time);
return b;
}
static long Duration() throws ParseException {
String a = TimeIn();
String b = TimeOut();
SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss");
Date date1 = format.parse(a);
Date date2 = format.parse(b);
long dur = date2.getTime() - date1.getTime();
return dur / 1000;
}
static String a;
static String b;
Your code gets two times.
After that , you calculate the difference and divide it by 1000.
The problem is that the times are in milliseconds as long values.
The times may be calculated in less than a millisecond and may therefore be equal as long cannot store decimals.
Also the division by 1000 makes it worse as it erases all differences that are smaller than a second.
In other words, the time difference between the call of TimeIn and TimeOut has to be at least one second if the result should not be 0.
[Note]
By convention, method names should start with a lowercase letter in java.
You make one measurement immediately after the other.
Consider the following scenario:
String a = TimeIn();
Thread.sleep(2000L);
String b = TimeOut();
Then the output is: 2
It seems like you call TimeIn right after TimeOut. These execute in under a second, so that your dates (that are only precise to the second) are the same. Putting a Thread.sleep(100000) in between prints the correct duration:
// throw Exception for brevity
static long Duration() throws Exception{
String a=TimeIn();
Thread.sleep(10000);
String b=TimeOut();
// ...
}
For more precise duration measurements use System.currentTimeMillis() or even System.nanoTime():
long then = System.currentTimeMillis();
// do stuff
long duration = System.currentTimeMillis() - then;
This gives you the time in milliseconds. In this example, Duration() runs in 30-50 ms
Also, methods usually start with a small letter, like duration() or timeIn().
The time difference execution of the TimeIn() and TimeOut() is negligible. We can use Thread.sleep();
class Demo {
public static void main(String[] args) throws ParseException, InterruptedException {
System.out.println(Duration());
}
static String TimeIn() throws InterruptedException {
SimpleDateFormat format= new SimpleDateFormat("hh:mm:ss:SSS");
Date time= Calendar.getInstance().getTime();
Thread.sleep(2000);
a=format.format(time);
return a;
}
static String TimeOut(){
SimpleDateFormat format= new SimpleDateFormat("hh:mm:ss:SSS");
Date time= Calendar.getInstance().getTime();
b=format.format(time);
return b;
}
static long Duration() throws ParseException, InterruptedException {
String a=TimeIn();
String b=TimeOut();
SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss:SSS");
Date date1 = format.parse(a);
Date date2 = format.parse(b);
long dur = date2.getTime() - date1.getTime();
return dur/1000;
}
static String a;
static String b;
}
this code below gets the current time and timezone of the area
Date date = new Date();
DateFormat df = new SimpleDateFormat("HH:mm:ss");
df.setTimeZone(TimeZone.getDefault());
System.out.println("Time: " + df.format(date));
right now its 1:01 pm (at the time of typing)
what i need help doing is implementing a feature in the code that checks if the current time has passed, for example 1:00PM
but I have no idea where to even start, can you help me out?
Use the Java 8+ Time API class LocalTime:
LocalTime refTime = LocalTime.of(13, 0); // 1:00 PM
// Check if now > refTime, in default time zone
LocalTime now = LocalTime.now();
if (now.isAfter(refTime)) {
// passed
}
// Check if now >= refTime, in pacific time zone
LocalTime now = LocalTime.now(ZoneId.of("America/Los_Angeles"))
if (now.compareTo(refTime) >= 0) {
// passed
}
I see it has already answered with Time, but as a teaching point, if you really wanted to use Date, you could have done something like this:
public static void main(String[] args) {
Date date = new Date();
DateFormat df = new SimpleDateFormat("HH:mm:ss");
df.setTimeZone(TimeZone.getDefault());
System.out.println("Time: " + df.format(date));
//If you print the date you'll see how it is formatted
//System.out.println(date.toString());
//So you can just split the string and use the segment you want
String[] fullDate = date.toString().split(" ");
String compareAgainstTime = "01:00PM";
System.out.println(isPastTime(fullDate[3],compareAgainstTime));
}
public static boolean isPastTime(String currentTime, String comparedTime) {
//We need to make the comparison time into the same format as the current time: 24H instead of 12H:
//then we'll just convert the time into only minutes to that we can more easily compare;
int comparedHour = comparedTime[-2].equals("AM") ? String.valueOf(comparedTime[0:2]) : String.valueOf(comparedTime[0:2] + 12 );
int comparedMin = String.valueOf(comparedTime[3:5]);
int comparedT = comparedHour*60 + comparedMin;
//obviously currentTime is alredy the correct format; just need to convert to minutes
int currentHour = String.valueOf(currentTime[0:2]);
int currentMin = String.valueOf(currentTime[3:5]);
int currentT = currentHour*60 + currentMin;
return (currentT > comparedT);
}
It's a bit messier, having to muddy into the Strings and whatnot, but it is possible. You would also have to be careful the zero-pad the comparedTime or just check for that in the function
I want to translate a date to human readable format. I am using DateUtils. getRelativeDateTimeString, but this does not fit the criteria. The output I am getting looks like: 1 hour, 15 min. ago, etc.
I want to know if it is possible to change the format to:
3m instead of 3 min. ago,
1h instead of 1 hour. 15 min. ago etc.
using DateUtils or is there another way to do this?
To be more precise I am looking for an Android equivalent of this angular-filter where you can easily change the format of the relative date (for example: {{minutes}} minutes ago to {{minutes}}m.
To make myself clear, I am not searching for a way to format a date, but to translate a date to human readable format such as 'today', '1 hr', '38 min' (simillar to facebook's relative dates).
After some research, I found out some libraries like Time4A, Joda-Time, PrettyTime, Android-Ago.
However, I have decided not to use a library and override its text resources, but to create a method and store text in strings.xml for possible future localization.
private static final int SECOND_MILLIS = 1000;
private static final int MINUTE_MILLIS = 60 * SECOND_MILLIS;
private static final int HOUR_MILLIS = 60 * MINUTE_MILLIS;
private static final int DAY_MILLIS = 24 * HOUR_MILLIS;
private static final int WEEK_MILLIS = 7 * DAY_MILLIS;
public static String getTimeAgo(Date date, Context context) {
Date now = Calendar.getInstance().getTime();
final long diff = now.getTime() - date.getTime();
if (diff < SECOND_MILLIS) {
return context.getString(R.string.just_now);
} else if (diff < MINUTE_MILLIS) {
return diff / SECOND_MILLIS + context.getString(R.string.seconds_ago);
} else if (diff < 2 * MINUTE_MILLIS) {
return context.getString(R.string.a_minute_ago);
} else if (diff < 59 * MINUTE_MILLIS) {
return diff / MINUTE_MILLIS + context.getString(R.string.minutes_ago);
} else if (diff < 90 * MINUTE_MILLIS) {
return context.getString(R.string.an_hour_ago);
} else if (diff < 24 * HOUR_MILLIS) {
return diff / HOUR_MILLIS + context.getString(R.string.hours_ago);
} else if (diff < 48 * HOUR_MILLIS) {
return context.getString(R.string.yesterday);
} else if (diff < 6 * DAY_MILLIS) {
return diff / DAY_MILLIS + context.getString(R.string.days_ago);
} else if (diff < 11 * DAY_MILLIS) {
return context.getString(R.string.a_week_ago);
} else {
return diff / WEEK_MILLIS + context.getString(R.string.weeks_ago);
}
}
Use the built-in DateUtils utility library that was included in API level 3.
CharSequence getRelativeDateTimeString (Context c,
long time,
long minResolution,
long transitionResolution,
int flags) Return string describing the elapsed time since startTime formatted like "[relative time/date], [time]".
Example output strings for the US date format:
3 min. ago, 10:15 AM Yesterday, 12:20 PM Dec 12, 4:12 AM 11/14/2007,
8:20 AM
You should use SimpleDateFormat and specify your desired template. Which in your case would be something like this:
String template = "H'h', m'm'";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(template, Locale.getDefault());
String formatted = simpleDateFormat.format(new Date());
Why don't you parse the Date to the format you want?
With string.replace() you can do it in one line of code.
Edit 1: I usually use SimpleDateForma this way, hope it helps:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String currentDateandTime = sdf.format(new Date());
you will need this imports ( i think that Android automatically imports):
import java.text.SimpleDateFormat;
import java.util.Date;
Edit 2: In your example what you have to do is:
SimpleDateFormat sdf = new SimpleDateFormat("HH'h' mm'm'");
String currentDateandTime = sdf.format(new Date());
System.out.println("Today in dd-MM-yy:HH:mm:SS : " + date);
For someone else who might stumble here, the link refered to in this question gives misleading results
My First Date: 1986-04-08. Current Date: 2013-11-28.
Code:
public long seconds(Date date){
String formattedDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",getResources().getConfiguration().locale).format(Calendar.getInstance().getTime());
String DateStr=String.valueOf(formattedDate);
Date d = null;
try {
d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",getResources().getConfiguration().locale).parse(DateStr);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
java.sql.Date dx = new java.sql.Date(d.getTime());
Date d1 = date;
Date d2 = dx;
t4.setText("BirthDate"+date+"\n Current Date:"+dx);
long seconds = (d2.getTime()-d1.getTime())/1000;
return seconds;
}
However when I check the results here: http://www.calculator.net/age-calculator.html?today=04%2F04%2F1986&ageat=11%2F28%2F2013&x=32&y=10 it gives me a slight different result. I am unsure where I am going wrong.
The online service you link to is wrong: it counts the age as whole days and then assumes that each day is exactly 24 hours long. Most of the time that's correct, but in most places in the world there are days with daylight savings time transitions and timezone transitions, meaning there have been days with 23, 25, or some other number of hours. The number you get from your Java code is more precise.
I think you're somehow mixing java.sql.Date and java.util.Date.
I would try simplifying the code. Something like this.
public class Test012 {
public static void main(String[] args) throws Exception {
System.out.println( seconds() );
System.out.println( seconds2() );
System.out.println( days3() );
}
public static long seconds() throws Exception {
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
java.util.Date d1 = sdf.parse("1986-04-08");
java.util.Date d2 = sdf.parse("2013-11-28");
return ( d2.getTime() - d1.getTime() ) / 1000;
}
public static long seconds2() throws Exception {
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
java.util.Date d1 = sdf.parse("1986-04-08");
java.util.Date d2 = new java.util.Date();
return ( d2.getTime() - d1.getTime() ) / 1000;
}
public static long days3() throws Exception {
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
java.util.Date d1 = sdf.parse("2008-01-01");
java.util.Date d2 = sdf.parse("2009-01-01");
return ( d2.getTime() - d1.getTime() ) / 1000 / 60 / 60 / 24;
}
}
I also tried
select datediff(ss, '4/8/1986', '11/28/2013') --- US date format
in SQL Server and it prints the same thing as this java program,
it prints 872294400. So this seems to be the correct value.
Are you sure the dates coming on your input are the right ones
(are equal to those I hardcoded in my test program)?
I would check that too.
Also, are you sure your dates have zero time parts? That's what the link/service you posted assumes.
Try this code:--
public static long secondsBetween(Calendar startDate, Calendar endDate) {
Calendar date = (Calendar) startDate.clone();
long daysBetween = 0;
while (date.before(endDate)) {
date.add(Calendar.DAY_OF_MONTH, 1);
daysBetween++;
}
return daysBetween*24*3600;
}
Hope it helps you.. Enjoy..!
I have the following method to sum time:
public static String sumTime(String date1, String date2) throws ParseException {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss.SSS");
Date d1 = formatter.parse(date1);
Date d2 = formatter.parse(date2);
calendar.setTime(d2);
d1 = DateUtils.addHours(d1, calendar.get(Calendar.HOUR_OF_DAY));
d1 = DateUtils.addMinutes(d1, calendar.get(Calendar.MINUTE));
d1 = DateUtils.addSeconds(d1, calendar.get(Calendar.SECOND));
d1 = DateUtils.addMilliseconds(d1, calendar.get(Calendar.MILLISECOND));
return formatter.format(d1);
}
DateUtils is from Apache Commons Lang 3
It works quite well for what I want, unless the sum is bigger than 24 hours.
For example:
String time = "00:00:00.000";
try {
for (int i = 0; i < 24; i++) {
time = sumTime(time, "01:00:00.123");
}
System.out.println(time);
} catch (ParseException e) {
e.printStackTrace();
}
The result is:
00:00:02.952
But this is what I'd like it to be:
24:00:02.952
Is there any (easy) way to accomplish that?
I don't mind using different libraries/methods, as long as I get the correct result.
Keep in mind that time will always start in 00:00:00.000;
Have you thought about using days to represent each set of 24 hours? You could add something in your sumTime method, and have it add days. SimpleDateFormater can use days, maybe this will help:
http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
java.util.Date is not so strong in this area. See the Joda Time for a library that handles this properly.
I don't have access to an installation just now. The code will be close to this:
DateTimeFormatter dtf = DateTimeFormat.forPattern("HH:mm:ss.SSS");
DateTime start = dtf.parseDateTime(date1);
DateTime end = dtf.parseDateTime(date2);
PeriodFormatter pf = new PeriodFormatterBuilder()
.printZeroAlways().appendHours().appendSeparator(":")
.appendMinutes().appendSeparator(":")
.appendSeconds().appendSeparator(":")
.appendMillis3Digit().toFormatter();
return pf.print(new Period(start, end, PeriodType.time()));
Date is not the right thing class to use. Date is a instant of time, not a "Date Difference".
The right thing to do will be to use a library like Joda Time as someone has already suggested. If you don't want to do so - here's a possible alternative:
Parse the string into hours, minutes and seconds yourself, and then add it yourself.
I would encourage you to look into a "well accepted" library though. There may be things I'm not thinking of in my solution. Also, you have add all the error checking.
Here's the starter code:
public class TimeInterval {
short milliSeconds;
short seconds;
short minutes;
int hours;
public TimeInterval (String dateString) {
// HHHHHH:MI:SS.SSS
Pattern pattern = Pattern.compile("(\\d+):(\\d\\d):(\\d\\d)\\.(\\d\\d\\d)");
Matcher matcher = pattern.matcher(dateString);
if ( matcher.find() ) {
hours = Integer.parseInt(dateString.substring(matcher.start(1), matcher.end(1)));
minutes = Short.parseShort(dateString.substring(matcher.start(2), matcher.end(2)));
seconds = Short.parseShort(dateString.substring(matcher.start(3), matcher.end(3)));
milliSeconds = Short.parseShort(dateString.substring(matcher.start(4), matcher.end(4)));
}
}
private TimeInterval() {
}
public TimeInterval add(TimeInterval interval) {
TimeInterval ret = new TimeInterval();
ret.milliSeconds = (short) ((interval.milliSeconds + milliSeconds)%1000);
int carry = (interval.milliSeconds + milliSeconds)/1000;
ret.seconds = (short) ((interval.seconds + seconds)%60 + carry );
carry =(interval.seconds + seconds)/60;
ret.minutes = (short) ((interval.minutes + minutes)%60 + carry);
carry = (interval.minutes + minutes)/60;
ret.hours = (interval.hours + hours + carry);
return ret;
}
#Override
public String toString() {
return String.format("%d:%02d:%02d.%03d", hours, minutes, seconds, milliSeconds);
}
}
Using this class your program will be like :
TimeInterval time = new TimeInterval("00:00:00.000");
try {
for (int i = 0; i < 24; i++) {
time = time.add(new TimeInterval("01:00:00.123"));
}
System.out.println(time.toString());
} catch (ParseException e) {
e.printStackTrace();
}
Have you tried Joda-Time which actually has direct support for this sort of thing?
PeriodFormatterBuilder builder = new PeriodFormatterBuilder();
builder.printZeroAlways()
.minimumPrintedDigits(2)
.appendHours()
.appendSeparator(":").appendMinutes()
.appendSeparator(":").appendSeconds()
.appendSeparator(".").appendMillis3Digit();
PeriodFormatter formatter = builder.toFormatter();
PeriodParser parser = builder.toParser();
String s1 = "11:00:00.111";
String s2 = "23:00:00.111";
MutablePeriod p1 = new MutablePeriod();
MutablePeriod p2 = new MutablePeriod();
parser.parseInto(p1, s1, 0, Locale.getDefault());
parser.parseInto(p2, s2, 0, Locale.getDefault());
p1.add(p2);
System.out.println(formatter.print(p1));
Prints
34:00:00.222