Parsing and checking validity of the date from numbers [duplicate] - java

This question already has answers here:
How can I pad an integer with zeros on the left?
(18 answers)
Closed 8 years ago.
I want to check the existence of the date, I have int variables ( representing format ) - day( dd ), month ( MM ) and year ( YY ). Here's my code:
SimpleDateFormat df = new SimpleDateFormat("YYMMdd");
df.setLenient(false);
try {
Date d = df.parse(""+day+month+year);
} ...
Because variables are in int I have problem when day for example can be 01 ( 1 digit in int ), or 21 ( 2 digits in int ) and I'm getting error as I have 'dd' in my format. Is there an easy way how I can use my numbers to check the validity of the date?

If you have the day, month and year in numeric format, it would be easier to create a date directly:
Calendar cal = Calendar.getInstance();
cal.clear(); //to set the time to 00:00:00.0000 if that is an issue
cal.set(year, month, day);
Date dt = cal.getTime();

You can pad your number strings like this:
String paddedDay = String.format("%02d", day);

Related

How to compare two dates in Java by incrementing Date? [duplicate]

This question already has answers here:
How can I increment a date by one day in Java?
(32 answers)
How to compare dates in Java? [duplicate]
(11 answers)
Closed 2 years ago.
How to compare two dates in Java by incrementing Date?
String dt = "2008-01-01"; // Start date
String et="2008-01-10";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
Date startDate=sdf.parse(dt);
Date endDate=sdf.parse(et);
Date incDate;
// dt is now the new date
do
{
System.out.println("hey i am called.....");
c.setTime(sdf.parse(dt));
c.add(Calendar.DATE, 1); // number of days to add
dt = sdf.format(c.getTime());
System.out.println("Incremet Date"+dt);
incDate=sdf.parse(dt);
}
while(endDate.compareTo(incDate)>=0);
first you should start using the newer classes like #TomStroemer pointed out.
I think you want to get the number of days between the two dates ?
LocalDate startDate = LocalDate.parse("2008-01-01");
LocalDate endDate = LocalDate.parse("2008-01-10");
Period period = Period.between(startDate, endDate);
System.out.println(period.getDays());
Should print 8 because there are 8 days between. Haven't tested that code.
See the following docs:
Period: https://docs.oracle.com/javase/8/docs/api/java/time/Period.html
LocalDate: https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
And really provide more details.

how can i get a month and year between two dates [duplicate]

This question already has answers here:
Calculating the difference between two Java date instances
(45 answers)
Closed 7 years ago.
I need to get difference between two dates using Java. I need my result to be in months and year of month.
Example:
Startdate = 2015-04-03 enddate = 2015-05-03 Result should be APR-MAY 2015
Startdate = 2015-12-03 enddate = 2015-01-03 Result should be DEC-2015,JAn-2016
i need to set that value into textview how can i set this plz help me .
String startdate = "2015-11-30";
String enddate = "2016-1-30";
DateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
DateFormat outputFormater = new SimpleDateFormat("MMM-yyyy");
Calendar beginCalendar = Calendar.getInstance();
Calendar finishCalendar = Calendar.getInstance();
try {
beginCalendar.setTime(formater.parse(startdate));
finishCalendar.setTime(formater.parse(enddate));
if (beginCalendar.get(Calendar.MONTH) != finishCalendar.get(Calendar.MONTH)){
beginCalendar.set(Calendar.DAY_OF_MONTH, 1);
finishCalendar.set(Calendar.DAY_OF_MONTH, 2);
}
do {
// add one month to date per loop
String month_year = outputFormater.format(beginCalendar.getTime());
Log.d("Date_Range", month_year);
beginCalendar.add(Calendar.MONTH, 1);
} while (beginCalendar.before(finishCalendar));
} catch (ParseException e) {
e.printStackTrace();
}
So by this you will get month and year between start date and end date in MMM-yyyy format. You can handle the result in the way you want by splitting month_year string # "-" separator.
You can use SimpleDateFormat.
EDIT: Try this
SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
Check if your dates are in the same year by getting the year of the calendar.
int year1=Integer.pareInt(formatter.format(calendar1.getTime()));
int year2=Integer.pareInt(formatter.format(calendar2.getTime()));
year=year1-year2;
and then print result based on the year
formatter=new SimpleDateFormat("MMM");
if(year==0)
System.out.println(formatter.format(calendar1)+"-"+formatter.format(calendar2)+" "+ year);
else
System.out.println(formatter.format(calendar1)+"-"+year1+","+formatter.format(calendar2)+"-"+year2);

Java SimpleDateFormat.format Throws java.lang.IllegalArgumentException [duplicate]

This question already has answers here:
Android : Error SimpleDateFormat Unknown pattern character 'u'
(3 answers)
Closed 7 years ago.
I need to get "Day number of week" from Date object. I tried to parse the Day number from the Date object as new SimpleDateFormat("u").format(date).
According to the Oracle documentation of the SimpleDateFormat u : Day number of week (1 = Monday, ..., 7 = Sunday)
This is the exception details
Try this to get the day of the date...
Calendar c = Calendar.getInstance();
c.setTime(yourDate);//Replace your date with the date which u have
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
Use This
Calendar c = Calendar.getInstance(); //create an instance of Calendar class
int Day = c.get(Calendar.DAY_OF_WEEK); //get current day of the week from the same instance

Get next days date in Java [duplicate]

This question already has answers here:
How to add days to a date in Java
(3 answers)
Closed 7 years ago.
I have an app in which I need to get the year, month and the day seperate from the next day as an String.
Can I do tis somehow with
SimpleDateFormat day = new SimpleDateFormat("dd");
String Day = day.format(new Date()+1);
or how can I get those? Please help me I'm a total beginner.
SimpleDateFormat sdFormat = new SimpleDateFormat("dd");
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
String day = sdFormat.format( calendar.getTime() )
Newer answer for Java 8 using time API.
LocalDate tomorrow = LocalDate.now().plusDays(1);
int year = tomorrow.getYear();
String month = tomorrow.getMonth().toString();
int dayOfMonth = tomorrow.getDayOfMonth();

SimpleDateFormat mmddyyyy returns improper data for month [duplicate]

This question already has answers here:
getting month name wrong with SimpleDateFormat
(2 answers)
Closed 9 years ago.
I want a timestamp field. I tried the below to get time in mmddyyyyhhmmss format.
All the fields give proper data but the month field has improper value.
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mmddyyyyhhmmss");
String date = simpleDateFormat.format(new Date());
O/P I get is 13132013021331. What I expect is 03133013021331.
Your Month should be MM not mm
mm means Minutes
SimpleDateFormat mmddyyyy returns improper data for month
coz your format for month is wrong.
it should be:
new SimpleDateFormat("MMddyyyyhhmmss");
MM---> Month
mm---> minutes
You need this:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMddyyyyhhmmss");
String date = simpleDateFormat.format(new Date());
You used "mm" twice - once for month (incorrect) and again for minutes (correct).

Categories

Resources