I am using below code for the date of birth in registration of the user, but when using it is getting the month of the birth one less, example birth month is September it is registering in database birth month as august, and this date of birth is being registered in numerics and format is dd/mm/yyyy
I want the accuracy with the month. please assist
public void showDateDialog() {
Calendar cal = Calendar.getInstance();
final int day = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH) ;
int year = cal.get(Calendar.YEAR);
DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
if (day < 10 && monthOfYear < 10)
date = "0" + dayOfMonth + "/0" + monthOfYear + "/" + year;
else if (day < 10 && monthOfYear > 10)
date = "0" + dayOfMonth + "/" + monthOfYear + "/" + year;
else if (day > 10 && monthOfYear < 10)
date = dayOfMonth + "/0" + monthOfYear + "/" + year;
else
date = dayOfMonth + "/" + monthOfYear + "/" + year;
dateOfBirth.setText(date);
}
};
DatePickerDialog dpDialog = new DatePickerDialog(this, listener, year, month, day);
dpDialog.show();
}
Calendar count month from 0 to 11.
So that you get one month difference.So you add 1 always.
int month = cal.get(Calendar.MONTH) + 1
Related
This code pick date next is triggered when a button is clicked; and then it adds +3 month to the picked date.
displayDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datepicker = new DatePickerDialog(Remainder.this, android.R.style.Theme_Holo_Light_Dialog_MinWidth, onDateSetListener, year,month,day);
datepicker.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
datepicker.show();
}
});
onDateSetListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int day) {
month = month+1;
Log.d(TAG, "onDateSet: mm/dd/yyyy: " + month + "/" + day + "/" + year);
String date = month + "/" + day + "/" + year;
displayDate.setText(date);
}
};
You can use Calendar class initialized with GregorianCalendar instance and then use the Calendar's add() method to add months to your date.
And then use get() method to:
get the day of the month by: calendar.get(Calendar.DAY_OF_MONTH)
get the month (range 0-11) by: calendar.get(Calendar.MONTH)
get the year by: calendar.get(Calendar.YEAR)
To apply this to your code:
onDateSetListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar calendar = new GregorianCalendar(year, month, day);
calendar.add(Calendar.MONTH, 3); // adding 3 months
int newMonth = calendar.get(Calendar.MONTH) + 1;
Log.d(TAG, "onDateSet: mm/dd/yyyy: " + newMonth + "/" + calendar.get(Calendar.DAY_OF_MONTH) + "/" + calendar.get(Calendar.YEAR));
String date = newMonth + "/" + calendar.get(Calendar.DAY_OF_MONTH) + "/" + calendar.get(Calendar.YEAR);
displayDate.setText(date);
}
};
I am trying to check that my user should be 18 years old. If Not then show a toast. (Trying to get from exact today date). But result is getting success only year wise.
Output - Years are getting calculated.
Expected - From today's date, user should be 18 years old.
this is what i have tried.
val calendar = Calendar.getInstance()
val year = calendar.get(Calendar.YEAR)
val month = calendar.get(Calendar.MONTH)
val day = calendar.get(Calendar.DAY_OF_MONTH)
val dpd = DatePickerDialog(this, DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
calendar.set(Calendar.YEAR, year)
calendar.set(Calendar.MONTH, monthOfYear)
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth)
val sdf = SimpleDateFormat(myFormat, Locale.US)
val dob = sdf.format(calendar.time)
val userAge = GregorianCalendar(year, month, day)
val minAdultAge = GregorianCalendar()
minAdultAge.add(Calendar.YEAR, -18)
minAdultAge.add(Calendar.MONTH, -1)
if (minAdultAge.before(userAge)) {
Toast.makeText(this, getString(R.string.txt_18_years_age_validation), Toast.LENGTH_SHORT).show()
} else {
etDob!!.setText(dob)
}
}, year, month, day
)
dpd.datePicker.maxDate = Calendar.getInstance().timeInMillis
dpd.show()
What modifications needed to get validations for todays date.
Thank You.
Try turning minAdultAge and the DOB into millis for comparing
minAdultAge.timeInMillis > dob.timeInMillis
I've edited to show how you could validate the exact age based on the day.
It's meant as a help not a solution.
public static void main(String[] args) {
Calendar present = Calendar.getInstance();
Calendar personBirthDate = Calendar.getInstance();
personBirthDate.set(Calendar.YEAR, 2001);
personBirthDate.set(Calendar.DAY_OF_YEAR, personBirthDate.get(Calendar.DAY_OF_YEAR) - 1); // yesterday
int yearDiff = present.get(Calendar.YEAR) - personBirthDate.get(Calendar.YEAR);
int dayDiff = present.get(Calendar.DAY_OF_YEAR) - personBirthDate.get(Calendar.DAY_OF_YEAR);
System.out.println("Day of person birth year " + personBirthDate.get(Calendar.DAY_OF_YEAR));
System.out.println("Day of current year " + present.get(Calendar.DAY_OF_YEAR));
System.out.println("Years between " + yearDiff);
if(present.get(Calendar.DAY_OF_YEAR) - personBirthDate.get(Calendar.DAY_OF_YEAR) > 0){
System.out.println("You are only " + (yearDiff - 1) + " years old");
}else if(present.get(Calendar.DAY_OF_YEAR) - personBirthDate.get(Calendar.DAY_OF_YEAR) < 0) {
System.out.println("You are already " + yearDiff + " years old");
}else{
System.out.println("You are exactly " + yearDiff + " years old");
}
}
Hi All I have a requirement where i need to check date with system date value but the date value i have in DB is in milliseconds so below are my code but the problem is the system date which i get display month value incorrect its display month value one month less .
Date now = new Date();
Long nowLong = now.getTime() / 1000;
Integer unixTime = nowLong.intValue();
Calendar cal;
cal = Calendar.getInstance();
cal.setTimeInMillis(unixTime * 1000L);
Integer day = cal.get(Calendar.DAY_OF_MONTH);
Integer month = cal.get(Calendar.MONTH);
Integer hour = cal.get(Calendar.HOUR);
Integer minute = cal.get(Calendar.MINUTE);
String amPm = (cal.get(Calendar.AM_PM) == Calendar.PM) ? "PM" : "AM";
Integer year = cal.get(Calendar.YEAR);
System.out.println("day :" + day);
System.out.println("month :" + month);
System.out.println("hour " + hour);
System.out.println("minute " + minute);
System.out.println("amPm " + amPm);
System.out.println("year " + year);
For this the output is
day :3
month :4
hour 3
minute 13
amPm PM
year 2016
but output is not correct . Can any one help me .
In Java the value returned by cal.get(Calendar.MONTH)is zero bazed. Thenf JANUARY = 0, FEBRUARY = 0, ... (documentation here)
Try this :
Integer month = cal.get(Calendar.MONTH) + 1;
Or this :
System.out.println("month :" + (month.intValue() + 1));
Note the creation of your calendar can be simpler:
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
try using LocalDate . there has been lot of changes to Date Time api in java 8 .you can do it better with LocalDate.
LocalDate today = LocalDate.now(); //current date
today.getMonth() //getMonth as a string
today.getMonth()//getMonthValue in an int
today.getDayOfWeek() // get the date in a string format
today.getDayOfMonth() //get the date as an int.
today.getYear()//get the year
Why does it show strike line on getDate(), getMonth() and getYear(). These methods are used to get current date, month and year but I don't know why it shows strike on these methods.
Code:
public class hello {
public static void main(String[] args) {
int days;
int month;
int year;
days = 24;
month = 10;
year = 1994;
System.out.println("Date of Birth: " + days + "/" + month + "/" + year);
Date d = new Date();
int t = d.getDate();
int x = d.getMonth() + 1;
int f = d.getYear() + 1900;
System.out.println("Current Date: " + t + "/" + x + "/" + f);
}
}
IDEs like Eclipse would strike the methods if they are deprecated, meaning they're not recommended for use because there is a better alternative. See the Javadocs of getDate():
Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.DAY_OF_MONTH).
Using Calendar methods:
Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_MONTH);
int month = calendar.get(Calendar.MONTH) + 1;
int year = calendar.get(Calendar.YEAR);
That's becuase they're deprecated. If you set the #deprecated in the info above a function, it'll strike methods over in most IDE's.
These specific functions are deprecated because the newer Calendar is a better option.
Try this.
int days;
int month;
int year;
days=24;
month=10;
year=1994;
System.out.println("Date of Birth: "+days+ "/" +month+ "/" +year);
LocalDate dd = LocalDate.of(year, month, days);
System.out.println("Current Date: " + dd);
System.out.println("Month: " + dd.getMonth());
System.out.println("Day: " + dd.getDayOfMonth());
System.out.println("Year: " + dd.getYear());
//If you would add year
LocalDate newYear = dd.plusYears(10);
System.out.println("New Date: " + newYear);
This is output:
Date of Birth: 24/10/1994
Current Date: 1994-10-24
Month: OCTOBER
Day: 24
Year: 1994
New Date: 2004-10-24
I am using Calendar function to set my custom date to calendar. I am setting it like below this but it is giving different date.
int day = Integer.parseInt(String.valueOf(dOutput.getDwDay()));
int monthday = Integer.parseInt(String.valueOf(dOutput.getDwMonth()));
int monthyearday = Integer.parseInt(String.valueOf(dOutput.getDwYear()));
System.out.println("day = " + day);
System.out.println("monthday = " + monthday);
System.out.println("monthyearday = " + monthyearday);
System.out.println("After setting Time: " + calendar.getTime());
calendar.set(Calendar.DATE, day);
calendar.set(Calendar.DAY_OF_MONTH, monthday);
calendar.set(Calendar.DAY_OF_YEAR, monthyearday);
int frommonth = calendar.get(Calendar.MONTH);
int year = calendar.get(Calendar.YEAR);
System.out.println("year = " + year);
System.out.println("frommonth = " + frommonth);
OUTPUT
I am giving this
day = 23
monthday = 5
monthyearday = 2014
But it is generating like this:
year = 2019
frommonth = 6
You are setting the wrong fields on your calendar. Set the fields like this:
calendar.set(Calendar.DAY_OF_MONTH, day); // day
calendar.set(Calendar.MONTH, monthday); // month
calendar.set(Calendar.YEAR, monthyearday); // year