How to get date from milliseconds - java

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

Related

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

Java Show Strike Line on Methods

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

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