This question already has answers here:
How do I do calendar arithmetic with java.util.Date?
(6 answers)
Closed 4 years ago.
What is the best way to make operation on a util.date Object in java.
For example:
I have a date 2018.10.02 and i want to add 3 Month to this date and to get another util.date object with the good date.
The same with adding or substracting day, years or hours...
Thanks
You can use java.time package since java8, for example:
Date date = new Date();
Instant instant = Instant.ofEpochMilli(date.getTime());
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault());
zonedDateTime = zonedDateTime.plusMonths(3);
Date afterThreeMonth = Date.from(zonedDateTime.toInstant())
New class java.time.LocalDate is better for that kind of operations:
LocalDate date = LocalDate.parse("2018-10-02").plusMonths(3);
Related
This question already has answers here:
long timestamp to LocalDateTime
(6 answers)
Java Converting 19-digit Unix Timestamp to a Readable Date
(3 answers)
Closed 4 years ago.
Is there any library i can use in java to properly convert 19 digit unix timestamp in the proper human readable date format in java ?
Eg:
1547111550416874183
1547111550917748553
You've got a timestamp in nanoseconds there, by the looks of it. (If it's not in nanos, adjust the 1_000_000_000 accordingly).
Split it into seconds and nanos:
long seconds = timestamp / 1_000_000_000;
long nanos = timestamp % 1_000_000_000;
Then construct a java.time.Instant:
Instant instant = Instant.ofEpochSecond(seconds, nanos);
Then you've got the whole java.time API available to do whatever you need to with it.
You can use this :
SimpleDateFormat sdf = new SimpleDateFormat("MMMM d, yyyy 'at' h:mm a");
String date = sdf.format(myTimestamp);
Hope it helps !
This question already has answers here:
Parsing ISO-8601 DateTime with offset with colon in Java
(4 answers)
Closed 6 years ago.
Hi I have time string format : 2017-02-20 09:57:08.512534+00
How to change above format to "dd//MM/yyyy" format in java?
Convert String to date object using parse method
then format date object using format method as per your requirement
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat format2 = new SimpleDateFormat("dd/MM/yyyy");
Date date = format1.parse("2017-02-20 09:57:08.512534+00");
System.out.println(format2.format(date));
Do yourself the favour of using the Java 8 java.time classes if you can use Java 8:
String dateTime = "2017-02-20 09:57:08.512534+00";
String date = LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSSSSx"))
.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
System.out.println(date);
This prints
20/02/2017
At face value it looks pretty much the same as the version using the now obsolete classes (Date and SimpleDateFormat). Still. For one thing, for your own good you will want to learn to use the new classes, not the legacy ones. Also, if some day you want to something else with the date than just convert from one string representation to another, the versatility and wealth of options of LocalDateTime and friends is likely to be useful.
DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Date date = format.parse(myString);
This question already has answers here:
convert String in time to Time object without Date
(3 answers)
Closed 6 years ago.
I am trying to convert a 6 digits String to Time without the date with SimpleDateFormat but I am getting the date of 01.01.1970 after converting the time. How can I just get the time stored in the time variable without the date?
Code
String timeString = "004500";
SimpleDateFormat formater = new SimpleDateFormat("hhmmss");
Date time= formater.parse(timeString );
you can use LocalTime which is what you want. You can parse a standard date like this:
String str = "1986-04-08 12:30";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
LocalTime time - dateTime.toLocalTime();
If you only have a time to parse you can use LocalTime.parse() method.
Note that this is only available starting from Java 8. You can also add Joda Time as a dependency if you are not using Java 8 yet.
This question already has answers here:
Convert Date/Time for given Timezone - java
(16 answers)
Closed 8 years ago.
I have user input date with TimeZone "GMT +5:30". But JVM is using "GMT +0:00". I want to convert Date from "GMT +5:30" to "GMT +0:00".
Thanks
Date in Java dont have a timezone. You need to try like this:
SimpleDateFormat df = new SimpleDateFormat();
df.setTimeZone(TimeZone.getTimeZone("GMT+5.30"));
System.out.println(df.format(c.getTime()));
Also you can use the Joda-Time
This question already has answers here:
String to LocalDate
(5 answers)
Closed 8 years ago.
I have a String in the form "20140518". How to convert it into LocalDate object
I tried this
this.todayDate = new LocalDate(val);
System.out.println(todayDate.toString("yyyy-mm-dd"))
When I try dumping this to standard output it dumps like 20140518-junk-junk. That it dumps a garbage string . I thought it would dump like 2014-05-18.
Use MM that represents Month instead of mm that represents minutes.
Use LocalDate.parse() instead of new LocalDate() to construct the LocalDate object.
DateTimeFormatter format = org.joda.time.format.DateTimeFormat.forPattern("yyyyMMdd");
LocalDate lDate = org.joda.time.LocalDate.parse("20140518", format);
System.out.println(lDate);
output:
2014-05-18
org.joda.time.LocalDate#toString() be default uses yyyy-MM-dd pattern.
You don't need to use todayDate.toString("yyyy-MM-dd").