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);
Related
This question already has answers here:
java.util.Date is generating a wrong date?
(3 answers)
Java SimpleDateFormat always returning January for Month
(4 answers)
Java Date() giving the wrong date [duplicate]
(9 answers)
Closed 1 year ago.
I am trying to convert "2019-04-24" to "24-Apr-2019" this but it is converting it as "24-Jan-19".
This is the code I am using:
public static String dateFormatConvert(String date, String srcDateFormat, String targetDataFormat) throws ParseException {
DateFormat dateFormat = new SimpleDateFormat(srcDateFormat);
Date srcDate = dateFormat.parse(date);
DateFormat destDateFormat = new SimpleDateFormat(targetDataFormat);
return destDateFormat.format(srcDate);
}
Calling it:
String date1 = CommonUtil.dateFormatConvert("2019-04-24", "yyyy-MM-DD", "DD-MMM-YY"); -> This is one of the format used (DD-MMM-YY) out of many
System.out.println(date1);
What's going wrong here?
According to the Documentation.
D - Day in year
d - Day in month
The correct code would be:
date1 = dateFormatConvert("2019-04-24", "yyyy-MM-dd", "dd-MMM-yyyy");
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)
This question already has answers here:
Month in simple date formatter always return JANUARY [duplicate]
(5 answers)
SimpleDateFormat ignoring month when parsing
(4 answers)
Closed 4 years ago.
I will get straight to the point, my goal it so change
2018-05-24
Into
May 24, 2018.
Here is my current code:
String content = "2018-05-24";
SimpleDateFormat old = new SimpleDateFormat("yyyy-mm-dd");
Date oldDate = old.parse(content);
SimpleDateFormat current = new SimpleDateFormat("MMMM dd, yyyy.");
String newDate = current.format(oldDate);
System.out.println(newDate);
But running that produces:
January 24, 2018.
I am not sure why this is telling me January. I know I have it wrong, I have searched StackOverFlow for similar questions, but maybe I am not doing something right. Any advice?
It's because you're using mm for month when it should be MM:
SimpleDateFormat old = new SimpleDateFormat("yyyy-MM-dd");
This outputs:
May 24, 2018.
This question already has answers here:
Change date format in a Java string
(22 answers)
Closed 7 years ago.
I want to represent date in the format MM-DD-YYYY as Day, Month Date
For example - represent 5-12-2015 as Tuesday, May 12. How can I do this?
Though I am giving you the code to do this, you should first show us your efforts.
String date = "5-12-2015";
DateFormat format = new SimpleDateFormat("MM-dd-yyyy");
Date d = format.parse(date);
DateFormat format1 = new SimpleDateFormat("EEEE, MMM dd");
String finalDateString = format1.format(d);
System.out.println(finalDateString);
This question already has answers here:
How to Convert Date to Int? [closed]
(2 answers)
Closed 9 years ago.
I'm Trying to get the current day value (example. 12) then assign it to a variable (example. today= 12).
DateFormat DateFormat= new SimpleDateFormat ("dd");
//get current day time with Date()
Date day= new Date ();
int day1 = Integer.valueOf(day);
Or
DateFormat DateFormat= new SimpleDateFormat ("dd");
//get current day time with Date()
Date day= new Date ();
int day1 = day;
But it didn't work :(
There's another way?
Sorry for not clearing the meaning enough in my preview question :)
Apart from #R.J answer, you can use a Calendar and get the day:
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
int day = calendar.get(Calendar.DATE);
You can do something like this
int day1 = Integer.valueOf(DateFormat.format(day));
But I must say the naming convention is very bad.