How to add minutes to a date variable in java [duplicate] - java

This question already has answers here:
How to add 10 minutes to my (String) time?
(8 answers)
Closed 8 years ago.
I have a date variable (endTime) with some value (eg : 10:40). I need to create a new variable by adding 10 minutes to endTime. How can I do it?
Thanks in advance
public static String endTime = "";
DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
Calendar cal = Calendar.getInstance();
endTime = timeFormat.format(cal.getTime());`

You can use add method on your Calendar:
public static String endTime = "";
DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, 10)
endTime = timeFormat.format(cal.getTime());`
Found in Javadoc, it takes me 30 seconds.

Related

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 subtract two days from a date in Java? [duplicate]

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

Convert HTML Time into Java Time Object [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 6 years ago.
I am getting startTime and endTime value from html5 input type. I am getting it in Servlet in String.
I want to convert it into Java Date Object so I can use methods like before and after for comparing Time.
String startTimeValue = request.getParameter("startTime");
String endTimeValue = request.getParameter("endTime");
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
Date startTime = sdf.parse(startTimeValue);
Date endTime = sdf.parse(endTimeValue);
sdf.format(startTime);
sdf.format(endTime);
System.out.println(endTime.before(startTime));
Any help would be appreciated.
I think you're able to do the parsing right this way :
String DateString = request.getParameter("date");
//SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
Date date = sdf.parse(myDateString);
Calendar calendar = GregorianCalendar.getInstance(); // creates a new calendar instance
calendar.setTime(date); // assigns calendar to the given date
int hour = calendar.get(Calendar.HOUR);
int minute; /... similar methods for minutes and seconds
I am achieving it using the following Code Snippet
String startTimeValue = request.getParameter("startTime");
String endTimeValue = request.getParameter("endTime");
String[] time1 = startTimeValue.split(":");
Date date1 = new Date();
date1.setHours(Integer.parseInt(time1[0]));
date1.setMinutes(Integer.parseInt(time1[1]));
date1.setSeconds(0);
String[] time2 = endTimeValue.split(":");
Date date2 = new Date();
date2.setHours(Integer.parseInt(time2[0]));
date2.setMinutes(Integer.parseInt(time2[1]));
date2.setSeconds(0);
System.out.println(date2.after(date1));

How do I add 2 weeks to a Date in java? [duplicate]

This question already has answers here:
Modify the week in a Calendar
(4 answers)
Closed 5 years ago.
I am getting a Date from the object at the point of instantiation, and for the sake of outputting I need to add 2 weeks to that date. I am wondering how I would go about adding to it and also whether or not my syntax is correct currently.
Current Java:
private final DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
private Date dateOfOrder;
private void setDateOfOrder()
{
//Get current date time with Date()
dateOfOrder = new Date();
}
public Date getDateOfOrder()
{
return dateOfOrder;
}
Is this syntax correct? Also, I want to make a getter that returns an estimated shipping date, which is 14 days after the date of order, I'm not sure how to add and subtract from the current date.
Use Calendar and set the current time then user the add method of the calendar
try this:
int noOfDays = 14; //i.e two weeks
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateOfOrder);
calendar.add(Calendar.DAY_OF_YEAR, noOfDays);
Date date = calendar.getTime();
I will show you how we can do it in Java 8. Here you go:
public class DemoDate {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
System.out.println("Current date: " + today);
//add 2 week to the current date
LocalDate next2Week = today.plus(2, ChronoUnit.WEEKS);
System.out.println("Next week: " + next2Week);
}
}
The output:
Current date: 2016-08-15
Next week: 2016-08-29
Java 8 rocks !!
Use Calendar
Date date = ...
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.WEEK_OF_MONTH, 2);
date = c.getTime();
Try this to add two weeks.
long date = System.currentTimeMillis() + 14 * 24 * 3600 * 1000;
Date newDate = new Date(date);
if pass 14 to this addDate method it will add 14 to the current date and return
public String addDate(int days) throws Exception {
final DateFormat dateFormat1 = new SimpleDateFormat(
"yyyy/MM/dd HH:mm:ss");
Calendar c = Calendar.getInstance();
c.setTime(new Date()); // Now use today date.
c.add(Calendar.DATE, addDays); // Adding 5 days
return dateFormat1.format(c.getTime());
}
Using the Joda-Time library will be easier and will handle Daylight Saving Time, other anomalies, and time zones.
java.util.Date date = new DateTime( DateTimeZone.forID( "America/Denver" ) ).plusWeeks( 2 ).withTimeAtStartOfDay().toDate();
If you are on java 8 you can use new date time api http://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html#plusWeeks-long-
if you are on java 7 or more old version of java you should use old api http://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#add-int-int-

How to get time on Android phone programmatically? [duplicate]

This question already has answers here:
How to get current time and date in Android
(42 answers)
Closed 8 years ago.
I am using this Java method here to get the current time:
final Date d = new Date();
d.getTime();
1390283202624
What I am getting a numeric figure of datatype long. What I need is the exact time in the format hh:mm:ss. And in the end I also have to perform arithmetic on the figure obtained.
Any clue? Also is this a reliable way of obtaining time on Android phone because I am getting a constant value here?
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
String formattedDate = sdf.format(d);
System.out.println(formattedDate);
Calendar instance = Calendar.getInstance();
int hour = instance.get(Calendar.HOUR);
int minute = instance.get(Calendar.MINUTE);
int second = instance.get(Calendar.SECOND);
use:
SimpleDateFormat sdf=new SimpleDateFormat("hh:mm:ss");
String time=sdf.format(new Date());
Use Calendar class.
Calendar c = Calendar.getInstance();
int seconds = c.get(Calendar.SECOND);
See this question. Calendar class contain all desired information.

Categories

Resources