How to subtract two days from a date in Java? [duplicate] - java

This question already has answers here:
How can I increment a date by one day in Java?
(32 answers)
Closed 6 years ago.
String dateSample = "2016-09-30 21:59:22.2500000";
String oldFormat = "yyyy-MM-dd HH:mm:ss";
String newFormat = "MM-dd-yyyy";
SimpleDateFormat sdf1 = new SimpleDateFormat(oldFormat);
SimpleDateFormat sdf2 = new SimpleDateFormat(newFormat);
sdf2.format(sdf1.parse(dateSample));
From this, I got 09-30-2016
But, I want the result 09-28-2016
How to do it?

Calendar cal = GregorianCalendar.getInstance();
cal.setTime( sdf1.parse(dateSample));
cal.add( GregorianCalendar.DAY_OF_MONTH, -2); // date manipulation
System.out.println(sdf2.format(cal.getTime()));
Hope I helped

Related

How to compare two dates in Java by incrementing Date? [duplicate]

This question already has answers here:
How can I increment a date by one day in Java?
(32 answers)
How to compare dates in Java? [duplicate]
(11 answers)
Closed 2 years ago.
How to compare two dates in Java by incrementing Date?
String dt = "2008-01-01"; // Start date
String et="2008-01-10";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
Date startDate=sdf.parse(dt);
Date endDate=sdf.parse(et);
Date incDate;
// dt is now the new date
do
{
System.out.println("hey i am called.....");
c.setTime(sdf.parse(dt));
c.add(Calendar.DATE, 1); // number of days to add
dt = sdf.format(c.getTime());
System.out.println("Incremet Date"+dt);
incDate=sdf.parse(dt);
}
while(endDate.compareTo(incDate)>=0);
first you should start using the newer classes like #TomStroemer pointed out.
I think you want to get the number of days between the two dates ?
LocalDate startDate = LocalDate.parse("2008-01-01");
LocalDate endDate = LocalDate.parse("2008-01-10");
Period period = Period.between(startDate, endDate);
System.out.println(period.getDays());
Should print 8 because there are 8 days between. Haven't tested that code.
See the following docs:
Period: https://docs.oracle.com/javase/8/docs/api/java/time/Period.html
LocalDate: https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
And really provide more details.

Java wrong data format transformation [duplicate]

This question already has answers here:
Get integer value of the current year in Java
(16 answers)
Closed 5 years ago.
I need a Java program that subtracts 5 years from the current year.
Everything is working fine but after I run the program:
DateFormat dateFormat = new SimpleDateFormat("yyyy");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR,-5);
Date today = new Date();
String start = dateFormat.format(cal.getTime()).toString();
String end = dateFormat.format(today).toString();
double start_doub = Double.parseDouble(start);
double end_doub = Double.parseDouble(end);
System.out.println(start_doub);
System.out.println(end_doub);
The result is:
2012.0
2017.0
I don't know the reason why the program adds .0 after the year?
How can I remove the last part?
Your code look like below
DateFormat dateFormat = new SimpleDateFormat("yyyy");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR,-5);
Date today = new Date();
String start = dateFormat.format(cal.getTime()).toString();
String end = dateFormat.format(today).toString();
int start_doub = Integer.parseInt(start);
int end_doub = Integer.parseInt(end);
System.out.println(start_doub);
System.out.println(end_doub);

How to count down 6 month in android? [duplicate]

This question already has answers here:
android java parse date from string
(3 answers)
Closed 5 years ago.
I have a date string in this format '20161201'.
How to make this a datetime string to be july 2016?
I want to show 6 months before that datetime string.
Is it possible?
Also how do I convert just '20161201' to Dec 2016?
you change the format using the following code,
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd", Locale.getDefault());
SimpleDateFormat expectedSDF = new SimpleDateFormat("MMMM yyyy", Locale.getDefault());
Date date = simpleDateFormat.parse(dateString);
String newFormatString = expectedSDF.format(date);
And for the countdown, use CountDownTimer
https://developer.android.com/reference/android/os/CountDownTimer.html
For convert '20161201' to 'Dec 2016':
//Convert string to date
String dateString = "20161201";
SimpleDateFormat parseDateFormat = new SimpleDateFormat("yyyyMMdd");
Date convertedDate = new Date();
try {
convertedDate = parseDateFormat.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
//Convert date to string
SimpleDateFormat outputDateFormat = new SimpleDateFormat("MMM yyyy");
String result = outputDateFormat.format(convertedDate);
//Print the result
System.out.println(result);
In '6 months' to millisecond is 15778476000. You can get the date after 6 months in millisecond by using
long resultdate = convertedDate.getTime() - 15778476000L;
and then
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(resultdate);
String result = outputDateFormat.format(calendar.getTime());
to get the result
https://developer.android.com/reference/java/text/SimpleDateFormat.html
https://developer.android.com/reference/java/util/Date.html
https://developer.android.com/reference/java/util/Calendar.html#setTimeInMillis(long)
how to make this datetime string to be july 2016
I want to show 6 months before that datetime string
July is 5 months...
Here's the 6 month answer
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(new SimpleDateFormat("yyyyMMdd").parse("20161201"));
c.set(Calendar.MONTH, c.get(Calendar.MONTH)-6);
String output = new SimpleDateFormat("MMMM yyyy").format(c.getTime()));
// June 2016

How to convert the string 11-07-1995 to Friday 7 1995?? Android [duplicate]

This question already has answers here:
Convert String Date to String date different format [duplicate]
(8 answers)
Closed 6 years ago.
I can convert the string to date, but when I want to convert the string in other type of data it is wrong.
String string = "1995 11 07";
SimpleDateFormat fmt = new SimpleDateFormat("cccc F MMMM yyyy", Locale.getDefault());
date = fmt.parse(string);
Toast.makeText(getContext(), date.toString(), Toast.LENGTH_LONG).show();
What is the correct practice?
String input_date="01/08/2012";
SimpleDateFormat format1=new SimpleDateFormat("dd/MM/yyyy");
Date dt1=format1.parse(input_date);
DateFormat format2=new SimpleDateFormat("EEEE");
String finalDay=format2.format(dt1)

Get next days date in Java [duplicate]

This question already has answers here:
How to add days to a date in Java
(3 answers)
Closed 7 years ago.
I have an app in which I need to get the year, month and the day seperate from the next day as an String.
Can I do tis somehow with
SimpleDateFormat day = new SimpleDateFormat("dd");
String Day = day.format(new Date()+1);
or how can I get those? Please help me I'm a total beginner.
SimpleDateFormat sdFormat = new SimpleDateFormat("dd");
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
String day = sdFormat.format( calendar.getTime() )
Newer answer for Java 8 using time API.
LocalDate tomorrow = LocalDate.now().plusDays(1);
int year = tomorrow.getYear();
String month = tomorrow.getMonth().toString();
int dayOfMonth = tomorrow.getDayOfMonth();

Categories

Resources