This question already has answers here:
How can I increment a date by one day in Java?
(32 answers)
Closed 7 years ago.
I'm trying to figure out how to increment dates in JAVA.
The date I'm trying to increment is, 2012-10-01.
The following represents the increments:
2012-10-01 - 2013-09-30
2013-10-01 - 2014-09-30
2014-10-01 - 2015-09-30
2015-10-01 - 2016-09-30
With the new Java time API you can use a LocalDate:
LocalDate date = LocalDate.parse("2012-10-01");
for (int i = 0; i < 4; i++) {
System.out.println(date + " - " + date.plusYears(1).minusDays(1));
date = date.plusYears(1);
}
Please try this,
Calendar c = Calendar.getInstance();
c.setTime(randomDate);
c.add(Calendar.YEAR, n);
newDate = c.getTime();
It is here
Use following code
import java.util.Calendar;
import java.text.SimpleDateFormat;
public class HelloWorld {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
cal.set(Calendar.YEAR, 2010);
cal.set(Calendar.MONTH,9); //Month start with 0=> Jan
cal.set(Calendar.DATE,01);
System.out.println(dateformat.format(cal.getTime()));
cal.add(Calendar.YEAR,1);
cal.add(Calendar.DATE,-1);
System.out.println(dateformat.format(cal.getTime()));
}
}
how about use joda-time.jar
e.g
public static String getTargetDate(String date)
{
DateTime dt = new DateTime(date);
DateTime dt2 = dt.plusYears(1);
dt2 = dt2.minusDays(1);
return dt2.toString().substring(0, 10);
}
Related
This question already has answers here:
Adding n hours to a date in Java?
(16 answers)
Closed 5 years ago.
I got Codes below, it generates my current time.
public static void main(String[] args) {
long timeInMillis = System.currentTimeMillis();
Calendar cal1 = Calendar.getInstance();
cal1.setTimeInMillis(timeInMillis);
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss a");
String dateforrow = dateFormat.format(cal1.getTime());
System.out.println(dateforrow );
}
Now How can I add hours to the current time? Like for example my current time 4:30:00 PM , and I want to add 8 hours to it, so maybe the output is 0:30:00 AM. I have no Idea.
You can try something like,
public static void main(String[] args) {
long timeInMillis = System.currentTimeMillis();
Calendar cal1 = Calendar.getInstance();
cal1.setTimeInMillis(timeInMillis);
cal1.add(Calendar.HOUR, 8);
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss a");
String dateforrow = dateFormat.format(cal1.getTime());
System.out.println(dateforrow );
}
long timeInMillis = System.currentTimeMillis();
Calendar cal1 = Calendar.getInstance();
cal1.setTimeInMillis(timeInMillis);
Date date = cal1.getTime();
//add two hour
date.setHours(date.getHours()+2);
cal1.setTime(date);
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss a");
String dateforrow = dateFormat.format(cal1.getTime());
System.out.println(dateforrow );
add two hour
This question already has answers here:
How to get previous date in java
(11 answers)
Closed 7 years ago.
I am trying to get date which is 5 days from today, my code snippet looks like this -
Date curDate = new Date();
out.println(curDate);
String pattern = "yyyy.MM.dd";
DateFormat format1 = new SimpleDateFormat(pattern, Locale.ENGLISH);
//String DateToStr = format1.format(curDate);
String DateToStr = format1.format(curDate);
out.println(DateToStr);
Date date = format1.parse(DateToStr);
Date prevdate = DateUtils.addDays(Date(), -5);
Though getting the current date 2016.02.19, i am not able to nail 5 days before which is 2016.02.14.
Any suggestions what's that i am doing wrong here? Or any better way to do this?
Appreciate your help.
Arun
import java.util.*;
import java.text.*;
public class HelloWorld
{
public static void main(String[] args)
{
java.text. SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar c = Calendar.getInstance();
c.setTime(new Date()); // Now use today date.
c.add(Calendar.DATE, -5); // 5 days back date
String output = sdf.format(c.getTime());
System.out.println(output);
}
}
This question already has answers here:
Java string to date conversion
(17 answers)
Closed 8 years ago.
How can I get day and date from given Strings. For example:
String date="25-12-2014";
How to get date and day from given string?
Expected output is,
25
Thu
I got stuck when I tried this.
private static String getFormatedDate(String strDate) {
String result = "";
if(strDate != null) {
if (strDate.contains("-")) {
String[] dates = strDate.split("-");
for(int i=0;i<dates.length;i++) {
result = result + Utils.replaceDateFormat(dates[i].trim(),"MMM dd", "EE, M.dd") + ("-");
}
int lastIndex = result.lastIndexOf("-");
result = result.substring(0, lastIndex).trim();
}
else {
result = Utils.replaceDateFormat(strDate.trim(),"MMM dd", "EE, M.dd");
}
}
return result;
}
Utils:
public static String replaceDateFormat(String value, String actualFormat, String exceptedFormat) {
final int currentYear = Calendar.getInstance().get(Calendar.YEAR);
final SimpleDateFormat fromDate = new SimpleDateFormat(actualFormat);
final SimpleDateFormat toDate = new SimpleDateFormat(exceptedFormat);
Date convertedFromDate = null;
try {
convertedFromDate = fromDate.parse(value);
} catch (java.text.ParseException e) {
e.printStackTrace();
}
final Calendar c1 = Calendar.getInstance();
c1.setTime(convertedFromDate);
c1.set(Calendar.YEAR, currentYear);
return toDate.format(c1.getTime());
}
Your methods are very convoluted for a relatively simple task. Why don't you use SimpleDateFormat? You can use the parse method. For example:
Date date = new SimpleDateFormat("dd-MM-yyyy").parse(string);
And then you can get the required fields from there.
EDIT
To get the day of the week, you were right with this code:
Date d = date.parse(result);
Calendar c = Calendar.getInstance();
c.setTime(d);
int day=c.get(Calendar.DAY_OF_WEEK);
And then if you want it in the format above, you could just make an array filled with the days of the week:
String[] daysOfWeek = new String[]{"Sun","Mon"... etc}
String day = daysOfWeek[day - 1];
You can use the method from Calendar:
String date = "25-12-2014";
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
Calendar cal = Calendar.getInstance();
cal.setTime(format.parse(date));
int day = cal.get(Calendar.DAY_OF_MONTH);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
DateFormatSymbols symbols = new DateFormatSymbols(new Locale("en"));
String[] days = symbols.getShortWeekdays();
System.out.printf("%02d %3s\n", day, days[dayOfWeek]);
The symbols can be set to your Locale zone.
if you are allowed to use java 8 you can give LocalDate a chance:
String date = "25-12-2014";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate ld = LocalDate.parse(date, formatter);
System.out.println(ld.getDayOfMonth() + ", " + ld.getDayOfWeek());
Output is:
25, THURSDAY
EDIT:
System.out.println(ld.getDayOfMonth() + ", " + ld.getDayOfWeek().substring(0, 3));
#No aNoNym suggestion is right, with the following you get
25, THU
This question already has answers here:
how to add days to java simple date format
(2 answers)
Closed 8 years ago.
I need to increment date by some days.
private Date now = new Date();
private Date result;
public void incrementDate(Integer days) {
result =
}
So if days equals 3 i need to increment my now date on 3 days and set it to result.
I know that java 8 has plusDays method in LocalDate class. Is there a way how to implement this in java 7.
Use Calendar
Calendar cal = Calendar.getInstance ();
cal.setTime (now);
cal.add (Calendar.DATE, days);
plus other fun stuff.
Use Calendar to do this:
Calendar cal = new GregorianCalendar();
cal.add(Calendar.DATE,3);
result = cal.getTime()
I suggest you make the function static and pass in now. return Date and use a Calendar. Something like,
public static Date incrementDate(Date now, int days) {
Calendar cal = Calendar.getInstance();
cal.setTime(now);
cal.add(Calendar.DAY_OF_MONTH, days);
return cal.getTime();
}
And then to test it
public static void main(String[] args) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date now = new Date();
System.out.println(df.format(now));
System.out.println(df.format(incrementDate(now, 3)));
}
Output here (today) is
2014-11-12
2014-11-15
try this code :
SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
Calendar cal=Calendar.getInstance();
String today=sdf.format(cal.getTime());
System.out.println(today);
cal.add(Calendar.DATE, 20);
String After=sdf.format(cal.getTime());
System.out.println(After);
Date now = new Date();
This question already has answers here:
Calendar date to yyyy-MM-dd format in java
(11 answers)
Closed 8 years ago.
I require this format MonthDay, example 0422
I'm creating
SimpleDateFormat sdf = new SimpleDateFormat("MMdd");
and giving it the current month and current day
curDate = sdf.parse(curMon+""+curDay);
but I'm getting this format:
Thu Jun 07 00:00:00 CEST 1973
What do I need to do?
Instead of using parse, use format as follows:
SimpleDateFormat s = new SimpleDateFormat("MMdd");
String d = s.format(new Date());
System.out.println(d);
This will generate, since today is 27th May:
0527
I hope below code will help you...
strDate="2014-08-19 15:49:43";
public String getMonth(String strMonth) {
int month = Integer.parseInt(strMonth.substring(0, 2));
int day = Integer.parseInt(strMonth.substring(strMonth.length() - 2,
strMonth.length()));
String d = (new DateFormatSymbols().getMonths()[month - 1]).substring(
0, 3) + " " + day;
return d;
}
public static String smallDate(String strDate) {
String str = "";
try {
SimpleDateFormat fmInput = new SimpleDateFormat("yyyy-MM-dd");
Date date = fmInput.parse(strDate);
SimpleDateFormat fmtOutput = new SimpleDateFormat("MMdd");
str = fmtOutput.format(date);
str = getMonth(str);
Log.d("Output date: "+str);
return str;
} catch (Exception e) {
}
}
use like that
Date date; // your date
Calendar cal = Calendar.getInstance();
cal.setTime(date);
String month = cal.get(Calendar.MONTH).toString();
String day = cal.get(Calendar.DAY_OF_MONTH).toString();
String mmdd=month+""+day;