Java LocalDate Formatting [duplicate] - java

This question already has answers here:
How to format LocalDate object to MM/dd/yyyy and have format persist
(4 answers)
Closed 5 years ago.
Looking for some help with LocalDate, I have so far come up with this:
public static String getDate(int days) {
LocalDate localDate = LocalDate.of(2017, 03, 04).plusDays(days);
return localDate.toString():
}
I need to return it as a string, but in this format: dd/mm/yyyy = 29/04/1991.
How can I achieve this?

use DateTimeFormatter:
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("dd/MM/yyyy" );
return LocalDate.of(2017, 03, 04).plusDays(days).format(formatter);

Related

Convert 201204121458 to date format 2012/04/12 14:59 in Java [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Change date format in a Java string
(22 answers)
Closed 3 years ago.
I have an issue with java how can convert string 201204121458 to date format 2012/04/12 14:59 using a method ?
Thank you
This code snippet may help:
String str = "201204121458";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmm");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
DateTimeFormatter printer = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
String formattedDateTime = dateTime.format(printer); // "2012/04/12 14:59"

Formatting LocalDate in Java [duplicate]

This question already has answers here:
Get first and last day of month using threeten, LocalDate
(13 answers)
How to format LocalDate to yyyyMMDD (without JodaTime)
(2 answers)
Closed 3 years ago.
I defined the following format in Java :
//define format of YYYYMMDD
private final DateTimeFormatter dtf = DateTimeFormatter.BASIC_ISO_DATE;
My application fetches a certain date from the calendar:
LocalDate localDate = fetchDate(); // fetch date in format "yyyy-mm-dd"
I want to store an additional two dates in format of dtf. The startOfMonth and endOfMonth of the given localDate.
E.g. if localDate is "2019-12-12" I want to create the following two variables -
String startOfMonth = "20191201";
String endOfMonth = "20191231";
How can I do that?
LocalDate atStartOfMonth = localDate.with(TemporalAdjusters.firstDayOfMonth());
LocalDate atEndOfMonth = localDate.with(TemporalAdjusters.lastDayOfMonth());
And then you format those local dates the way you want to (i.e. with your dtf formatter)

How to get today's Date in java in the following pattern dd/MM/yyyy? [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
Change date format in a Java string
(22 answers)
Convert java.util.Date to String
(17 answers)
Closed 3 years ago.
I am trying the code
Date today = new Date();
Date todayWithZeroTime;
{
try {
todayWithZeroTime = formatter.parse(formatter.format(today));
} catch (ParseException e) {
e.printStackTrace();
}
}
String date = todayWithZeroTime.toString();
it is giving output :- Wed Dec 11 00:00:00 IST 2019
where I want 11/12/2019
where 11/12/2019 is today's date
Using java.time.LocalDate,
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate localDate = LocalDate.now();
System.out.println(dtf.format(localDate)); //2016/11/16
Use DateTimeFormatter to format the date as you want.
In your case the pattern is "dd/MM/yyyy".
Info ⬇️
Java 8 introduced new APIs for Date and Time to address the shortcomings of the older java.util.Date and java.util.Calendar. The core classes of the new Java 8 project that are part of the java.time package like LocalDate, LocalTime, LocalDateTime, ZonedDateTime, Period, Duration and their supported APIs.
The LocalDate provides various utility methods to obtain a variety of information. For example:
1) The following code snippet gets the current local date and adds one day:
LocalDate tomorrow = LocalDate.now().plusDays(1);
2) This example obtains the current date and subtracts one month. Note how it accepts an enum as the time unit:
LocalDate previousMonthSameDay = LocalDate.now().minus(1, ChronoUnit.MONTHS);
If you are using JAVA 8 you can use LocalDateTime class and DateTimeFormatter.
see below example using JAVA 8:
LocalDateTime now = LocalDateTime.now();
System.out.println("Current DateTime Before Formatting: " + now);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
String formatedDateTime = now.format(formatter);
System.out.println("Current DateTime after Formatting:: " + formatedDateTime );

How can I convert stringified date to date? [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
how to parse output of new Date().toString()
(4 answers)
Indonesian Times in SimpleDateFormat
(1 answer)
Closed 3 years ago.
I want to convert a string date object to Date object. In my request there is a field of the form "dateCreated":"Tue Jul 30 13:41:40 WIB 2019". I need to model it to Date class. How can I achieve this?
I thought the controller will automatically cast it to Date object. It didn't work. Also, simple casting to Date object from string also didnt work. I could not find a string formatter for this also.
private Date dateCreated = new Date();
So I want to convert a stringified Date object to Date object.
If you describe your format then you can do it with Joda time
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
DateTime dt = formatter.parseDateTime(string);
Well a slightly more complex problem of how do you parse date that can be in multiple formats, can be solved
DateTimeParser[] parsers = {
DateTimeFormat.forPattern("yyyy-MM-dd HH").getParser(),
DateTimeFormat.forPattern("yyyy-MM-dd").getParser() };
DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers).toFormatter();
DateTime date1 = formatter.parseDateTime( "2010-01-01" );
DateTime date2 = formatter.parseDateTime( "2010-01-01 01" );
https://www.joda.org/joda-time/
edit
From java 8 you can use
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate date1 = LocalDate.from(formatter.parse("2010-01-01"));

How to get date in days only Java? [duplicate]

This question already has answers here:
How to get day of year number in Java for a specific date object? [duplicate]
(3 answers)
Today is nth day of year [duplicate]
(6 answers)
Closed 5 years ago.
Is there a way to get the date in java in days only? For instance Jan 1 would equal 1 and Feb 1 would equal 32? Instead of the standard time format yyyy/mm/dd?
Perhaps by editing the date formater somehow:
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate localDate = LocalDate.now();
Use getDayOfYear():
LocalDate localDate = LocalDate.now();
System.out.println(localDate.getDayOfYear());
Returns: the day-of-year, from 1 to 365, or 366 in a leap year
You can use below
Calendar calendar = Calendar.getInstance();
int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
int day = LocalDate.getDayOfYear();
try this
SimpleDateFormat format1=new SimpleDateFormat("MM/dd/yyyy");
Date dt1=format1.parse(pass your date);
DateFormat format2=new SimpleDateFormat("EEEE");
String finalDay=format2.format(dt1);

Categories