Java Show Strike Line on Methods - java

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

Related

Updating Date inside a while loop, Java

Im trying to compare the real world date with a user input date within a while loop. Although the initial execute is correct, the second time its executing the date stays the same. Ive tried asking for the date inside the while loop and now most recently from within a class method but still the date stays the same.
How can I retrieve an up to date date?
import java.util.Date;
import java.util.Scanner;
class Watch {
Date CurrentTimeAndDate = new Date();
int CurrentMinutes() {
int currentMinutes = CurrentTimeAndDate.getMinutes();
System.out.println(currentMinutes);
return currentMinutes;
}
}
public class App {
public static void main(String[] args) throws InterruptedException {
Scanner input = new Scanner(System.in);
String timer = null;
int i = 0;
int num = 0;
Date TimeAndDate = new Date();
int getDay = TimeAndDate.getDay();
int getMonth = TimeAndDate.getMonth() + 1;
int getYear = TimeAndDate.getYear() + 1900;
int getMinutes = TimeAndDate.getMinutes();
Watch watch1 = new Watch();
String[] Month = { "", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
String[] Day = { "", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
System.out.println("Current time and date is " + TimeAndDate);
System.out.println("Printing my way! " + Day[getDay] + " " + Month[getMonth] + " " + getYear + " " + getMinutes);
System.out.println(" Enter a short description of what you want reminding about ");
String rem = input.nextLine();
System.out.println(" Enter date of reminder 1-7");
while (i < 7) {
System.out.println(i + 1 + " = " + Day[i + 1]);
i++;
}
int day = input.nextInt();
System.out.println("Enter Month of reminder");
i = 0;
while (i < 12) {
System.out.println(i + 1 + " " + "=" + " " + Month[i + 1]);
i++;
}
int month = input.nextInt();
System.out.println("Enter year");
int year = input.nextInt();
System.out.println("Enter Minutes, for testing purposes");
int userInputMinutes = input.nextInt();
System.out.println("Date set to remind you about " + rem + " " + Day[day] + " " + Month[month] + " " + year);
if (year > getYear) {
System.out.println("Its time to remind you about ");
} else {
System.out.println("Waiting");
}
int Mins = 0;
while (userInputMinutes != Mins) {
Mins = watch1.CurrentMinutes();
System.out.println("Current Minutes = " + getMinutes);
System.out.println("Entered minutes =" + userInputMinutes);
Thread.sleep(10000);
}
System.out.println("Its time to remind you about " + rem);
}
public static void Date(String time) {
}
}
You are setting the new Date() only once. So you will be getting that same in while loop iterations. To get a new date for every iteration, you have to set the below code inside the while loop
TimeAndDate = new Date();
int getDay = TimeAndDate.getDay();
int getMonth = TimeAndDate.getMonth() + 1;
int getYear = TimeAndDate.getYear() + 1900;
int getMinutes = TimeAndDate.getMinutes();
Watch watch1 = new Watch();
* Note: Date is a deprecated class. Please refer #Ole V.V. answer for
the correct class to use.*
First, use java.time, the modern Java date and time API, for you date and time work. Always. The Date class that you used (misused, really, I’ll get back to that) is poorly designed and long outdated. Never use that.
Getting current minutes
To get the current minute of the hour:
int currentMinutes() {
return LocalTime.now(ZoneId.systemDefault()).getMinute();
}
To read day of week or month from the user
Also use java.time for days of the week and for months. Your code is reinventing wheels. You should prefer to use library classes and methods that are already there for you. For example:
System.out.println(" Enter day of week of reminder 1-7");
for (DayOfWeek dow : DayOfWeek.values()) {
System.out.println("" + dow.getValue() + " = " + dow
.getDisplayName(TextStyle.SHORT_STANDALONE, Locale.ENGLISH));
}
int day = input.nextInt();
DayOfWeek dow = DayOfWeek.of(day);
System.out.println("You have chosen " + dow);
Example interaction:
Enter day of week of reminder 1-7
1 = Mon
2 = Tue
3 = Wed
4 = Thu
5 = Fri
6 = Sat
7 = Sun
2
You have chosen TUESDAY
Most methods of the Date class are deprecated for a reason
As I said, Date is poorly designed and long outdated. More than that, most of the constructors and methods of the class were deprecated in Java 1.1 in February 1997 because they work unreliably across time zones. So even if you insisted on using Date (which I hope you don’t), you should still stay far away from the deprecated methods including all of the get methods except getTime (which converts to milliseconds since the epoch).
Link
Oracle tutorial: Date Time explaining how to use java.time.

Date of birth issue in android

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

Check user is greater than 18 years from todays date with date picker android

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");
}
}

How to convert Date/Time from Long(millisecodns) to RFC-822 format in Java

I have time in milliseconds I need to convert it to RFC-822 format.
Is there generic Java library that can I use?
What is the best practice of doing it?
Example time in milliseconds: 1440612000000
Time in RFC-822: Wed, 02 Oct 2002 08:00:00 EST
Thanks in advance.
Use the SimpleDateFormat
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
dateFormat.setTimeZone(TimeZone.getTimeZone("EST")); //To use the EST time zone as in your example
dateFormat.format(new Date(timeInMiliseconds));
As an example, with 1440612000000L outputs Wed, 26 Aug 2015 13:00:00 EST.
You can use the calendar class included in Java:
Calendar calendar = Calendar.getInstance();
// set time in millis
calender.setTimeInMillis(millis);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH); // Jan = 0, dec = 11
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
int weekOfMonth= calendar.get(Calendar.WEEK_OF_MONTH);
int hour = calendar.get(Calendar.HOUR); // 12 hour clock
int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY); // 24 hour clock
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
int millisecond= calendar.get(Calendar.MILLISECOND);
System.out.println(sdf.format(calendar.getTime()));
System.out.println("year \t\t: " + year);
System.out.println("month \t\t: " + month);
System.out.println("dayOfMonth \t: " + dayOfMonth);
System.out.println("dayOfWeek \t: " + dayOfWeek);
System.out.println("weekOfYear \t: " + weekOfYear);
System.out.println("weekOfMonth \t: " + weekOfMonth);
System.out.println("hour \t\t: " + hour);
System.out.println("hourOfDay \t: " + hourOfDay);
System.out.println("minute \t\t: " + minute);
System.out.println("second \t\t: " + second);
System.out.println("millisecond \t: " + millisecond);
Then you can just build a string out of the values. E.g. :
String s = calender.getDisplayName(Calender.DAY_OF_WEEK, Calender.SHORT, locale) + ", " + calender.get(Calender.DAY_OF_MONTH) + " " + calender.getDisplayName(Calender.MONTH, Calender.SHORT, locale) + " " + calender.get(Calender.YEAR);
will print: Wed, 02 Oct 2002

Setting date to calendar instance in java

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

Categories