Java that adds time [duplicate] - java

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

Related

Java Date Increment [duplicate]

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

increment now date by several days issue in java [duplicate]

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

how to add 10 hour in current date and time in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
9/4/2014 3:55:10 AM
here is my current date and time i want to add +10 hour so that i cam match with Current time of device please tell me how we can implement
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
String currentDateandTime =" 9/4/2014 3:55:10 AM ";
Date date = formatter.parse(currentDateandTime);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR, 10);
System.out.println("Time here "+calendar.getTime());
}
This Code am trying but i am not able to Impalement please help me where am doing mistake .
Your code is almost working, but you have made a typo. You try to invoke parse() on formatter which has not been declared. Instead you have to call parse() on sdf:
public static void main(final String[] args) throws Exception {
final SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
final String currentDateandTime = " 9/4/2014 3:55:10 AM ";
final Date date = sdf.parse(currentDateandTime);
final Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR, 10);
System.out.println("Time here " + calendar.getTime());
}
Since you are using 12-hour system you can modify it like this:
public static void main(final String[] args) throws Exception {
final SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
final String currentDateandTime = "9/4/2014 3:55:10 AM";
final Date date = sdf.parse(currentDateandTime);
final Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR, 10);
System.out.println("Time here " + sdf.format(calendar.getTime()));
}
If you operate on dates after 1970, adding 10 hours to given date can be achieved in one line of code:
Date d1 = new Date(); // or sdf.parse()
Date d2 = new Date( d1.getTime() + 10 * 60 * 60 * 1000 ); // add 10h in millis
Output is:
Thu Sep 04 13:56:39 CEST 2014
Thu Sep 04 23:56:39 CEST 2014
First if you want to figure out the current time of your computers clock you can do the following:
long now = System.currentTimeMillis();
Which will return a long representing that timestamp. If you want it as a Date object you can do:
Date now = new Date();
As for the rest of your code the logic looks correct here is a snippet of my code that adds 10 hours to now.
Date now = new Date();
Calendar cal = new GregorianCalendar();
calendar.setTime(date);
calendar.add(Calendar.HOUR, 10);
Date plus10 = calendar.getTime();
System.out.println(plus10);
You didn't create a DateFormat. Try this:
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
public class Main {
public static void main(String[] args) throws ParseException {
String currentDateandTime = " 9/4/2014 3:55:10 AM ";
DateFormat formatter = DateFormat.getInstance();
Date date = formatter.parse(currentDateandTime);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR, 10);
System.out.println("Time here " + calendar.getTime());
}
}

Java : How to get Date rather than time?

Is it possible that i can get todays date , rather than time ??
This is my code
public static void main(String args[]) throws ParseException{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
Date today = calendar.getTime();
}
Why is todays date is shown as before date ??
public class Ravi {
public static void main(String args[]) throws ParseException {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
System.out.println("Todays Date"+dateFormat.format(date));
List currentObject = new ArrayList();
currentObject.add("2012-09-27");
Date ExpDate = dateFormat.parse((String) currentObject.get(0));
System.out.println("ddd"+ExpDate);
if (ExpDate.before(date)) {
System.out.println("true");
}
else {
System.out.println("false");
}
}
}
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
It will print 2012-09-27
Because Date is always the full current time e.g. 2012.09.27 12:45:23
Whilst your new Formated date is 2012.09.27 00:00:00 therefor the output is correct.
If you want to get false you will need to set hours, minutes and seconds to 0.
Using Calendar:
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime()));
Using Date:
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
Date date = new Date();
System.out.println(dateFormat.format(date));
Comparing Dates with Calendar:
Calendar old = Calendar.getInstance();
old.set(Calendar.YEAR, 2011);
Calendar now = Calendar.getInstance();
old.before(now));
Note you may want to set Hours Minutes and Seconds to 0.
Getting today date in yyyy-MM-dd format.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String output = sdf.format(new Date());
System.out.println(output);
Why is todays date is shown as before date ??
when you do dateFormat.parse("2012-09-27");
date what you will get it will be 00h00min00sec 2012-09-27
so when you compare it with new Date(); you will get today date but couple hours(and minutes, and seconds) later, and that is why "2012-09-27" is before new Date()
You can do something like this:
Date date=new Date();
SimpleDateFormat sd=new SimpleDateFormat("yyyyMMdd HHmmss");
String strDate=sd.format(date);
String dateNow=strDate.substring(0,strDate.indexOf(" "));
String timeNow=strDate.substring(strDate.indexOf(" ")+1);
Hope this helps.
public static void main(String args[]) throws ParseException{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
Date today = calendar.getTime();
sysout(dateFormat.format(today))
}

Get yesterday's date using Date [duplicate]

This question already has answers here:
How to check if a date Object equals yesterday?
(9 answers)
Closed 8 years ago.
The following function produces today's date; how can I make it produce only yesterday's date?
private String toDate() {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
return dateFormat.format(date).toString();
}
This is the output:
2012-07-10
I only need yesterday's date like below. Is it possible to do this in my function?
2012-07-09
Update
There has been recent improvements in datetime API with JSR-310.
Instant now = Instant.now();
Instant yesterday = now.minus(1, ChronoUnit.DAYS);
System.out.println(now);
System.out.println(yesterday);
https://ideone.com/91M1eU
Outdated answer
You are subtracting the wrong number:
Use Calendar instead:
private Date yesterday() {
final Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
return cal.getTime();
}
Then, modify your method to the following:
private String getYesterdayDateString() {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
return dateFormat.format(yesterday());
}
See
IDEOne Demo
You can do following:
private Date getMeYesterday(){
return new Date(System.currentTimeMillis()-24*60*60*1000);
}
Note: if you want further backward date multiply number of day with 24*60*60*1000 for example:
private Date getPreviousWeekDate(){
return new Date(System.currentTimeMillis()-7*24*60*60*1000);
}
Similarly, you can get future date by adding the value to System.currentTimeMillis(), for example:
private Date getMeTomorrow(){
return new Date(System.currentTimeMillis()+24*60*60*1000);
}
Calendar cal = Calendar.getInstance();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("Today's date is "+dateFormat.format(cal.getTime()));
cal.add(Calendar.DATE, -1);
System.out.println("Yesterday's date was "+dateFormat.format(cal.getTime()));
Use Calender Api
Try this one:
private String toDate() {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
// Create a calendar object with today date. Calendar is in java.util pakage.
Calendar calendar = Calendar.getInstance();
// Move calendar to yesterday
calendar.add(Calendar.DATE, -1);
// Get current date of calendar which point to the yesterday now
Date yesterday = calendar.getTime();
return dateFormat.format(yesterday).toString();
}
Try this;
public String toDate() {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
return dateFormat.format(cal.getTime());
}
changed from your code :
private String toDate(long timestamp) {
Date date = new Date (timestamp * 1000 - 24 * 60 * 60 * 1000);
return new SimpleDateFormat("yyyy-MM-dd").format(date).toString();
}
but you do better using calendar.
There is no direct function to get yesterday's date.
To get yesterday's date, you need to use Calendar by subtracting -1.
Calendar cal = Calendar.getInstance();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("Today's date is "+dateFormat.format(cal.getTime()));
cal.add(Calendar.DATE, -1);
System.out.println("Yesterday's date was "+dateFormat.format(cal.getTime()));

Categories