How to reduce one month from current date and stored in date variable using java?
Use Calendar:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, -1);
Date result = cal.getTime();
How to add one month to a date and get the same day
If you want get 3rd monday of month, then useset
instead of add
date.set(Calendar.DAY_OF_WEEK_IN_MONTH, 3);
if you want add one month to current date, usedate.add(Calendar.MONTH, 1);
EDIT
final Calendar date = Calendar.getInstance();
date.set(2012, Calendar.SEPTEMBER, 17);
int prevDayOfWeekInMonth = date.get(Calendar.DAY_OF_WEEK_IN_MONTH);
int prevDayOfWeek = date.get(Calendar.DAY_OF_WEEK);
date.add(Calendar.MONTH, 1);
date.set(Calendar.DAY_OF_WEEK, prevDayOfWeek);
date.set(Calendar.DAY_OF_WEEK_IN_MONTH, prevDayOfWeekInMonth);
add 1 month from current date using java.sql.Date
The problem with your solution is that you are not doing the calculations in long
.
You have to replace 31*24*60*60*1000
with 31l*24l*60l*60l*1000l
.
Here is the corrected code snippet:
public static void main (String[] args)
{
Date kini = new Date();
java.sql.Date jadwalPengobatan = new java.sql.Date(kini.getTime() + 31l*24l*60l*60l*1000l);
System.out.println(jadwalPengobatan);
}
Please note that this will only fix your calculation where you are adding 31
days to the current date. If you are looking to add a month which can be of 30
, 28
or 29
days also then perhaps you should make use of Calendar
.
Today's Date:
2016-02-27
Output:
2016-03-29
How add 30 days in current date?
You need to use
c1.add(Calendar.DAY_OF_YEAR, 30);
instead of
c1.add(Calendar.DATE, 30);
Try this
Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("YYYY-MM-dd");
Calendar c1 = Calendar.getInstance();
String currentDate = df.format(date);// get current date here
// now add 30 day in Calendar instance
c1.add(Calendar.DAY_OF_YEAR, 30);
df = new SimpleDateFormat("yyyy-MM-dd");
Date resultDate = c1.getTime();
String dueDate = df.format(resultDate);
// print the result
Utils.printLog("DATE_DATE :-> "+currentDate);
Utils.printLog("DUE_DATE :-> "+dueDate);
OUTPUT
2019-06-04 14:43:02.438 E/XXX_XXXX: DATE_DATE :-> 2019-06-04
2019-06-04 14:43:02.438 E/XXX_XXXX: DUE_DATE :-> 2019-07-04
Easiest way to add a month to a java DATE type without using Calendar or Joda?
Given that you have Date imported by default, you can add a number of months to the date object by the following:
public void addMonths(Date date, int numMonths){
date.setMonth((date.getMonth() - 1 + numMonths) % 12 + 1);
}
NOTE
You can use external classes from Java SE by using their full package name. i.e., even if you cannot add import java.util.Calendar;
to the top of you .java
file, you can still create a calendar object by executing java.util.Calendar cal = java.util.Calendar.getInstance();
Add some month to current date using java
As a starter: you should use prepared statements instead of concatenating variables in the query string, which is error-prone, unsafe and inefficient.
When it comes to doing date computation: you can do the arithmetics directly in the database. This is somewhat simpler than performing it in your application code.
In MySQL, if you want the current date plus 1 month, just do: current_date + interval 1 month
.
Here is an example of your statement, assuming that you want the new date in column date_fin
:
INSERT INTO `jardin`.`abonnement` (
`id`,
`enf_id`,
`data_debut`,
`date_fin`,
`type`,
`description`,
`statu`,
`statu_paiment`
) VALUES (
?,
?,
?,
current_date + interval 1 month,
?,
?,
?,
?
)
Alternatively, if you want to add one month to a date given as parameter:
INSERT INTO `jardin`.`abonnement` (
`id`,
`enf_id`,
`data_debut`,
`date_fin`,
`type`,
`description`,
`statu`,
`statu_paiment`
) VALUES (
?,
?,
?,
? + interval 1 month,
?,
?,
?,
?
)
PS: date parameters should be given in format YYYY-MM-DD
, with an optional time part (YYYY-MM-DD HH:MI:SS
).
Related Topics
Save JSON Outputted from a Url to a File
Advanced Java-Like Enums in Ruby
Translating Ruby Encryption Code to Java
How to Load a Jni .Dylib File with a Dependency Without Getting an Unsatifiedlinkerror
How to Encode String (Use Encrypt Messagedigest in Java) to Base64 String in Swift
Simple Calculator with Jsp/Servlet and Ajax
What Is an Index in Elasticsearch
Is String Interning Really Useful
How to Get Ruby Generated Hmac for Sha256 That Is Url Safe to Match Java
Java to Ruby Aes/Ecb/Pkcs5Padding Encryption
Jruby: Watir Is Hanging When Launching Browser
Simple Sso - Using Custom Authentication - Cas or Some Oauth or Openid Server
Can't Run Junit 4 Test Case in Eclipse Android Project
Wrong Version of Keystore on Android Call
How to Show Ellipses on My Textview If It Is Greater Than the 1 Line