This question already has answers here:
Calculating the difference between two Java date instances
(45 answers)
Closed 7 years ago.
I am working with an application for counting remaining days between two dates,ie
there is a start date and end date.I want to calculate remaining days to the end date in each day.Please help me.Thanks
SimpleDateFormat myFormat = new SimpleDateFormat("dd MM yyyy");
String inputString1 = "23 01 2015";
String inputString2 = "27 04 2015";
try {
Date date1 = myFormat.parse(inputString1);
Date date2 = myFormat.parse(inputString2);
long diff = date2.getTime() - date1.getTime();
System.out.println ("Days: " + TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS));
} catch (ParseException e) {
e.printStackTrace();
}
Related
This question already has answers here:
Comparing two times in android
(4 answers)
Convert String to java.util.Date
(4 answers)
Difference between java HH:mm and hh:mm on SimpleDateFormat
(5 answers)
Closed 1 year ago.
I am a little confused about what I see with the following test code:
public class TheDateIssue {
public static void main(String[] args) {
String TIME_FORMAT = "hh:mm aa";
try {
Date theDate = new SimpleDateFormat(TIME_FORMAT).parse("13:15 pm");
System.out.println("theDate: " + theDate);
} catch (ParseException e) {
System.out.println("Exception while converting string to Date. \n" + e.getLocalizedMessage());
}
}
The time to be parsed is "13:15 pm" - but the sysout output lists the time (highlighted below). I was expecting either 13:15:00 PST or 01:15:00 pm PST
theDate: Fri Jan 02 **01:15:00 PST** 1970
What am I doing wrong? :(
Try this:
LocalTime time = LocalTime.parse("13:15")
System.out.println(time.format(DateTimeFormatter.ofPattern("HH:mm a")));
Or:
ZonedDateTime time = ZonedDateTime.of(LocalDate.now(), LocalTime.parse("13:15"), ZoneId.of("America/Los_Angeles"));
System.out.println(time.format(DateTimeFormatter.ofPattern("HH:mm:ss zz")));
And if for some reason you are stuck with a date class, transform it somehow:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ssZ aa");
Date theDate = simpleDateFormat.parse("13:15:00 PST PM");
ZonedDateTime date = ZonedDateTime.ofInstant(theDate.toInstant(), ZoneId.of("America/Los_Angeles"));
System.out.println(date.format(DateTimeFormatter.ofPattern("HH:mm:ss zz")));
But you are right, it is weird behaviour. There are good reasons they created LocalDate etc, moving away from the Date class.
This question already has answers here:
Parsing a string to date format in java defaults date to 1 and month to January
(2 answers)
Closed 4 years ago.
I've spent a lot of time and I still do not understand why this is happening. In the code below, the time changes correctly, the date is not.
String dtAll = mDate + " " + mTime;
Date dat = new Date();
#SuppressLint("SimpleDateFormat") SimpleDateFormat format = new SimpleDateFormat("dd/MM/YYYY HH:mm");
try {
dat = format.parse(dtAll);
} catch (ParseException e) {
e.printStackTrace();
}
Log.d("dat", String.valueOf(dat));
Log.d("dtAll", dtAll);
Results:
D/dat: Sun Dec 31 17:24:00 GMT+01:00 2017
D/dtAll: 20/09/2018 17:24
Will anyone help me?
Use year in format yyyy:
new SimpleDateFormat("dd/MM/yyyy HH:mm");
This question already has answers here:
How to format date and time in Android?
(26 answers)
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
I have Date like this Sun May 20 09:18:44 GMT+04:30 2018 how i can get clock from this Date,
i want result like this 09:18 am
Log.v(TAG,"date "+ date.toString());// output is Sun May 20 09:18:44 GMT+04:30 2018
SimpleDateFormat sdf=new SimpleDateFormat("hh:mm");
String time=sdf.format(date);//NullPointerException
your code is working you just have to initialize Date class's object
Date date = new Date();
Date date = new Date();
Log.v(TAG,"date "+ date.toString());
SimpleDateFormat sdf=new SimpleDateFormat("hh:mm");
String time=sdf.format(date);
Log.v(TAG,"date "+ time);
Try this code..
SimpleDateFormat sdf3 = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
SimpleDateFormat sdf4 = new SimpleDateFormat("hh:mm", Locale.ENGLISH);
Date d1 = null;
try{
d1 = sdf3.parse("Sun May 20 09:18:44 GMT+04:30 2018");
String date=sdf4.format(d1);
System.out.println("time..." + date);
}catch (Exception e){ e.printStackTrace(); }
System.out.println("check..." + d1);
This question already has an answer here:
SimpleDateFormat("dd-MMM-YYYY") printing year one year ahead [duplicate]
(1 answer)
Closed 6 years ago.
I run the following code and obtain incorrect date (instead of 1/2/2015 6:00:00 AM it prints 12/28/2015 6:00:00 AM):
SimpleDateFormat _sdf = new SimpleDateFormat("M/d/YYYY H:mm:ss a");
_time = "02/01/2015 6:00:00 AM";
Date date;
try {
date = _sdf.parse(_time);
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(date);
_time = _sdf.format(calendar.getTime());
System.out.println(_time); // 12/28/2015 6:00:00 AM !!!
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Try to use this:
SimpleDateFormat _sdf = new SimpleDateFormat("M/d/yyyy H:mm:ss a");
i.e, you need to use yyyy instead if YYYY.
Check the Oracle Docs
y Year Year 1996; 96
Y Week year Year 2009; 09
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);