Problem: wrong output using Java Calendar class [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I am a Java developer.
I have a question about the Java Calendar library class.
What is my mistake? Is it a bug in Java?
Please explain to me.
public static void main(String[] args) {
Calendar cal2 = Calendar.getInstance();
Calendar cal3 = Calendar.getInstance();
DateFormat dateFormat = new SimpleDateFormat("dd/MM/YYYY");
cal3.add(Calendar.DATE, -1);
String today = (dateFormat.format(cal2.getTime()));
String yesterday = (dateFormat.format(cal3.getTime()));
System.out.println(today);
System.out.println(yesterday);
}
Output:
01/01/2019
Picked up _JAVA_OPTIONS: -Xmx512M
31/12/2019
2019 is as expected in the first line, but I had expected 2018 in the last line.

Use yyyy(Year) for year instead YYYY(Week year). See the doc.

Related

SimpleDateFormat not working on 1629777600000 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Date format coming from the database: 1629777600000
SimpleDateFormat simpleDateFormatter = new SimpleDateFormat("MM/dd/yyyy");
simpleDateFormat.format(1629777600000);
When using this simple date format I am not able to convert 1629777600000 to a viewable date
SimpleDateFormat simpleDateFormatter = new SimpleDateFormat("MM/dd/yyyy");
System.out.println(simpleDateFormatter.format(1629777600000L));
Print: 08/24/2021
But your code does not compile:
you initialize simpleDateFormatter but use simpleDateFormat (without ter)
1629777600000 is a Long, therefore it needs an L at its end
I think the L problem is just from modifiying the code for the stackoverflow question. But the variable name is maybe a real problem in your code.

Convert from string "yyyyMMdd" to Calendar in java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I need to use Calendar class. This is my code, but it has an error certainly. How can I fix it?
String stringdate = "20161129";
String pattern = "yyyyMMdd";
Date date = new SimpleDateFormat(pattern).parse(stringdate);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
Your code looks good...
2 things can happen her:
you are not using the correct DAte class (import java.util.Date;)
you are not considering (catching or rethrowing ) the ParseException
in both cases you will get a compilation error...
like type mismatch
or unhandled exception

Why does not the Day change after adding 1 to the calendar? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have the following code:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, Integer.valueOf(timeOneHour));
calendar.set(Calendar.MINUTE,Integer.valueOf(timeOneMinute) );
calendar.set(Calendar.SECOND, 00);
Where timeOneHour = 10
and timeOneMinute = 20
Now I do the following:
System.out.println("On Day before adding: "+calendar.get(Calendar.DAY_OF_MONTH));
calendar.set(Calendar.DAY_OF_MONTH, (calendar.get(Calendar.DAY_OF_MONTH+1)));
System.out.println("On Day after adding: "+calendar.get(Calendar.DAY_OF_MONTH));
Both of the above println commands are printing out 2 - what is the mistake I am doing here?
Did you try incrementing the day like so : ?
calendar.add(Calendar.DAY_OF_MONTH, 1);
You have an error where +1 is added
calendar.set(Calendar.DAY_OF_MONTH, (calendar.get(Calendar.DAY_OF_MONTH+1)));
should be
calendar.set(Calendar.DAY_OF_MONTH, (calendar.get(Calendar.DAY_OF_MONTH)+1));
You are acutally adding one to the constant, DAY_OF_MONTH, which happens to be DAY_OF_YEAR. For january they are the same.)

String to date Conversion in Java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this code
SimpleDateFormat ft = new SimpleDateFormat ("dd/MM/yyyy");
String sDate="2013-11-13";
String formattedDate=ft.format( ft.parse(sDate));
but this not working. Any help
You need to change the date format like this
SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd");
Edit
comment question : but I need dd/mm/yyyy format?
Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2013-11-13");
String formattedDate = new SimpleDateFormat("dd/MM/yyyy").format(date);
System.out.println(formattedDate);
Change SimpleDate Format to below, and it shall work
SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd");

Format for date object with am/pm [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I was wondering how I can just set a time such as 10:30am to a Date object from java.util.date? How would I format this? For example Date date1 = 10:30am.
try with the Calendar class then output using SimpleDateFormat
SimpleDateFormat format = new SimpleDateFormat("HH:mma");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 30);
System.out.println(format.format(calendar.getTime()));
can't do that.Only format to String representations.
SimpleDateFormat simDateFormate = new SimpleDateFormat("hh:mm a");
System.out.println(simDateFormate.format(new Date()));

Categories