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