Incorrect date obtained after processing [duplicate] - java

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

Related

android parse string to date - incorrect date [duplicate]

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

How to get clock from a date [duplicate]

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

Java WEEK_OF_YEAR [duplicate]

This question already has answers here:
Why dec 31 2010 returns 1 as week of year?
(6 answers)
Closed 5 years ago.
I implemented the following method:
private int getWeek(String datum){
SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
Date date = null;
try {
date = format.parse(datum);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone( "Europe/Berlin" ));
calendar.setTime(date);
int week = calendar.get(Calendar.WEEK_OF_YEAR);
return week;
}
But when I call the method with
getWeek("01.01.2017")
it returns 52. But it should be 1.
Where is my mistake?
When i call the method with
getWeek("01.01.2016")
it returns 53.
you have lost set the TimeZone in SimpleDateFormat, for example:
private int getWeek(String datum) {
TimeZone zone = TimeZone.getTimeZone("Europe/Berlin");
SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
// v--- set the timezone here
format.setTimeZone(zone);
Date date = null;
try {
date = format.parse(datum);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance(zone);
calendar.setTime(date);
int week = calendar.get(Calendar.WEEK_OF_YEAR);
return week;
}
Damnit.. that was a huge brain failure.
52 is the correct answer for the input.
in Germany the first Week of Year is the first week with 4 or more days in the new year.
Sorry.

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

Calculating days between two dates in android [duplicate]

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

Categories

Resources