I have two dates in String format like below – I want to make sure startDate should be less than endDate. startDate should not be greater than endDate. How can I compare these two dates and return boolean accordingly? Answer Convert them to an actual Date object, then call before. Recall that parse will throw a ParseException, so you should
Tag: calendar
Comparing dates with JUnit testing
Hello I’m new to the site and have a issue with my application using JUnit testing. My issue is when I try to compare the Date method with itself it always fails. I printed the Date object in the test to see the problem and always end up with the package name and random letters. Here is the Date constructor:
Working with various Calendar TimeZone in Java (without using Joda Time)
I was looking for a way to get current time in various timezones based on an user input. I know I could use Joda Time! but is that the only way? Isn’t there an option in Java for doing this? I tried the following code which gives the same output for all 3 sysouts. What am I missing here to
How to get the weekday of a Date?
I want to get the day of week from the Java Date object when I have an array of Date in String with me. Does anyone know the answer to this? Answer You can get the day-integer like that: If you need the output to be “Tue” rather than 3, instead of going through a calendar, just reformat the string:
Check if a given time lies between two times regardless of date
I have timespans: String time1 = 01:00:00 String time2 = 05:00:00 I want to check if time1 and time2 both lies between 20:11:13 and 14:49:00. Actually, 01:00:00 is greater than 20:11:13 and less than 14:49:00 considering 20:11:13 is always less than 14:49:00. This is given prerequisite. So what I want is, 20:11:13 < 01:00:00 < 14:49:00. So I need something
Creating a temporary copy of a Calendar object in Java
I need to figure out how to create a temporary Calendar object (a copy of a “permanent” calendar that already exists) so that I can manipulate the copy: tempCal.add(unit, value). I need to …
Java: Get week number from any date?
I have a small program that displays the current week from todays date, like this: And then a JLabel that displays the week number: So right now I’d like to have a JTextField where you can enter a date and the JLabel will update with the week number of that date. I’m really not sure how to do this as
Android Format date with time zone
I need to format the date into a specific string. I used SimpleDateFormat class to format the date using the pattern “yyyy-MM-dd’T’HH:mm:ssZ” it returns current date as “2013-01-04T15:51:45+0530” but I need as “2013-01-04T15:51:45+05:30”. Below is the coding used, Output: formatted string: 2013-01-04T15:51:45+0530 I need the format as 2013-01-04T15:51:45+05:30 just adding the colon in between gmt time. Because I’m working on
How to get last month/year in java?
How do I find out the last month and its year in Java? e.g. If today is Oct. 10 2012, the result should be Month = 9 and Year = 2012. If today is Jan. 10 2013, the result should be Month = 12 and Year = 2012. Answer Your solution is here but instead of addition you need to
java get week of year for given a date
how can I get a week of the year given a date? I tried the following code: and i obtain: sDateCalendar lun apr 23 11:58:39 CEST 2012 iStartWeek 2012 3 while the correct week of year is 17. Can someone help me ? Answer You are using sDateCalendar.WEEK_OF_YEAR, which is the static integer WEEK_OF_YEAR, see the source of the java.util.Calendar