This question already has answers here:
Parse Date from String in this format : dd/MM/yyyy [to dd/MM/yyyy]
(4 answers)
Closed 5 years ago.
so i need to take an input from User in the format of "MM/DD/YYYY"
and later read the day and month from the user input to use in some other calculations
i tried using strings and Gregorian calendar but i have no idea how to do that
Calendar cal = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
cal.setTime(dateFormat.parse("12/23/2017"));
Now you can get all kind of metrics form the calendar easily.
Related
This question already has answers here:
How to properly format the date?
(5 answers)
Get Date type object in format in java
(6 answers)
display Java.util.Date in a specific format
(11 answers)
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
I have looked around for help on this, but again, it's just one of those things that I cannot find a suitable answer to my specific issue.
Here's 2 very detailed (and helpful) SO posts that I've looked at:
Change date format in a Java string
Java string to date conversion
This is what I have:
//Date Formatter
SimpleDateFormat dateFormatter1 = new SimpleDateFormat("yyyy-MM-dd", new Locale("EN"));
SimpleDateFormat dateFormatter2 = new SimpleDateFormat("dd MMMM yyyy", new Locale("EN"));
//Convert to Date
Date dateToParse = dateFormatter1.parse("2024-01-01");
//Format OUTPUT date
String dateAsString = dateFormatter1.format(dateToParse);
System.out.println(dateAsString); //01 January 2024
//Convert OUTPUT date from STRING to DATE
Date dateToReturn = dateFormatter1.parse(dateAsString);
System.out.println(dateToReturn.toString()); //Mon Jan 01 00:00:00 SAST 2024
Note:
I used both DateFormatter and SimpleDateFormatter but got the same output.
The outputs are very different and what I am trying to achieve is to have my String created as a Date object in the exact same format.
I feel I am missing something but I just cannot figure out what.
The code I provided is a snippet from a bigger function that returns type Date
The function wasn't created by myself, I'm picking up from where someone else left off
Whenever you call a .toString() method on a Date, then the DateFormatter is not taken into account. The format of date you get is just a matter of default toString implementation of Date class.
To use formatter while printing, try something like this (instead of the last line in your snippet):
System.out.println(dateFormatter1.format(dateToReturn));
Date.toString() returns always returns a string in the format dow mon dd hh:mm:ss zzz yyyy.
You'll need to reuse the formatter's format method to get the initial string you expect
This question already has answers here:
How can I change the date format in Java? [duplicate]
(10 answers)
ISO 8601 String to Date/Time object in Android
(5 answers)
Closed 4 years ago.
How to convert the string to a date
In the string we have a timestamp (for example "2018-07-11T04:40:30Z"),
and I want to simply convert this String time to 11-07-2018 16:40:30
String Time = jresponse.getString("timestamp");
SimpleDateFormat sf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Date date = sf.parse(Time);
I tried many times this code and it doesn't work because I have always this format "2018-07-11T04:40:30Z" and not this 11-07-2018 16:40:30 why ?
If you want to parse dates like "2018-07-11T04:40:30Z"then you should change the format as "dd-MM-yyyy HH:mm:ss" will never work with that date. The correct format should be something like yyyy-MM-dd'T'HH:mm:ssZ.
This question already has answers here:
Java SimpleDateFormat always returning January for Month
(4 answers)
Closed 4 years ago.
I try to parse a date string, but get wrong month, why?
new SimpleDateFormat("yyyy-MM-DD", Locale.US).parse("2018-03-08")
Why this returns month as Jan?
Please check screenshot:
Why not use the java.time API?
LocalDate localDate = LocalDate.parse("2018-03-08");
If you want to convert the LocalDate to a java.util.Date, you can follow this answer.
This is due to the format you used as "yyyy-MM-DD". The parser will parse in the sequence way:
Your input value is "2018-03-08"
yyyy - will bring to the year 2018
MM - will bring to the month MARCH
But what is DD? It's the number of the days from the beginning of the year.
So here it moved back to 8th day on this year (2018) which means January 8th.
That's why you are seeing January instead of March.
Looking at this tutorial if you are trying to display the month name you should use 3 M’s and isn’t the standard for the day to be d and not D
I would suggest trying yyyy-MMM-dd
I tried this below and it is working fine by changing the DD to dd
try {
Date date= new SimpleDateFormat("yyyy-MM-dd",Locale.US).parse("2018-03-08");
Calendar calendar= Calendar.getInstance();
calendar.setTime(date);
Log.d("MONTH ","" + calendar.get(Calendar.MONTH));
} catch (ParseException e) {
e.printStackTrace();
}
You need to use dd instead of DD
Try this :-
new SimpleDateFormat("yyyy-MM-dd", Locale.US).parse("2018-03-08");
This question already has answers here:
Adding days to a date in Java [duplicate]
(6 answers)
Closed 5 years ago.
String s= new java.util.Date().toString();
I am using java 7, I want to print one week ahead of this date I have used the calendar class but it always gives me IST 1970 and does not give correct answer and I want that date into string so please help me in this. I know this question is repeated but they cant solve my query?
Don't use Date, use Calendar:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 7);
System.out.println(cal.getTime());
If you want date in string format then use Dateformat . Below is the complete code ,
Date givenDate= new Date();
DateFormat dateFormatter=new SimpleDateFormat("dd-MM-yy");
Calendar c=Calendar.getInstance();
c.setTime(givenDate);
//This will print whole week after given date
for(int i=1;i<8;i++){
c.add(Calendar.DAY_OF_MONTH, 1);
System.out.println(dateFormatter.format(c.getTime()));
}
This question already has answers here:
How to get Java to show the day of month from a date?
(4 answers)
Closed 6 years ago.
I'm trying to print a day of the given date. How can I do that?
String date = "2015.05.12";
SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
Date bornDate = format.parse(date);
System.out.println(bornDate.getDay());//this prints "2" instead of "12"
You can use bornDate.getDate() to get day. You shall use calendar class as methods on date are deprecated.