Michael
Michael

Reputation: 103

checking if time set is greater than current time?

The time will be saved as string, is there any way to convert the string into time and then check if its equal to or greater than current time, or if its less than current time?

Sorry if this has been asked many times before!

Upvotes: 2

Views: 8143

Answers (5)

belmel ahmed
belmel ahmed

Reputation: 356

//t1.toDate().before( Calendar.getInstance().getTime()
t1.toDate().after(  Calendar.getInstance().getTime()

Upvotes: 0

Basil Bourque
Basil Bourque

Reputation: 339432

Other answers are correct but outdated. Use java.time classes instead.

LocalTime

The LocalTime class represents a time-of-day.

LocalTime input = LocalTime.parse( "20:15" );

To get the current time-of-day, specify a time zone. For any given moment, the time-of-day varies by time zone.

ZoneId zoneId = ZoneId.of( "America/Montreal" );
LocalTime now = LocalTime.now( zoneId );

You can compare using the equals, compareTo, isBefore, and isAfter methods.

Boolean input isAfterNow = input.isAfter( now );

You can generate a String to represent this time-of-day value in standard ISO 8601 format with a 24-hour clock HH:MM:SS.SSSSSSSSS. Just call toString.

String output = now.toString();

java.time

The java.time framework is built into Java 8 and later. These classes supplant the old troublesome date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to java.time.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time.

Upvotes: 1

K_Anas
K_Anas

Reputation: 31466

This how you convert a string to date format:

I've supposed that you use the yyyy-MM-dd HH:mm:ss format

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd, HH:mm:ss");
formatter.setLenient(false); 

String oldTime = "2012-07-11 10:55:21";
Date oldDate = formatter.parse(oldTime);

This is how you get the current Time

Calendar c = Calendar.getInstance(); 
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentDate = df.format(c.getTime());

Finally This tutorial will show you how to compare dates in java

Upvotes: 4

HJW
HJW

Reputation: 23443

Use Java SimpleDateFormat class to convert from String to Date and compare from there.

String raw = "July 13 2012 08:43:12";

Date parsedDate= new SimpleDateFormat("MMMM d yyyy HH:mm:ss", Locale.ENGLISH).parse(raw);

Date currentDate = new Date();

boolean isafter = parsedDate.after(currentDate);

Upvotes: 0

karllindmark
karllindmark

Reputation: 6071

Is the time saved as a timestamp in String form, or an actual String? If it's the timestamp, you could always do the following:

long timestamp = Long.parseLong(stringTimestamp);

Upvotes: 1

Related Questions