Elegant way to convert "YYYY-MM" to Date object in Java [duplicate] - java

This question already has answers here:
Why my pattern("yyyyMM") cannot parse with DateTimeFormatter (java 8)
(2 answers)
Closed 5 years ago.
What is the best way to convert date in String format "YYYY-MM" to Date object in Java apart from String parsing.
What I tried is below.
String expirationDateArr[] = dateStr.split("-");
Then extract month and year to create the Date object.

Don't use Date. Since Java 8 we have time package which includes YearMonth class. With it your code can be as simple as
YearMonth ym = YearMonth.parse("2017-10");

Related

Convert date format from dd-mm-yyyy to YYMMDD [duplicate]

This question already has answers here:
How do I convert the date from one format to another date object in another format without using any deprecated classes?
(10 answers)
Closed 4 years ago.
How do I convert date format from
dd-mm-yyyy
to
YYMMDD
in java?
I think you are seeking the solution like date formatting. You can try to use DateTimeFormatter with your own pattern.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yy-MM-dd");

Java parse Date from string [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 5 years ago.
I've a problem with date. I've a date as string in format: "2017-05-10 16:30"
I'd like to convert it to date looking the same as I wrote before.
Please help me, thanks in advance!
You can use LocalDateTime.parse to create a LocalDateTime object, but the second part of your question didn't really make sense. The date object itself doesn't have a format, so it can't "look like" anything. You decide what format to adopt when you convert it back to a string.
A simple search in google or stackoverflow might lead to answer but here you go.
Pre Java 8
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse("2017-05-10 16:30");
Java 8
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime ldt = LocalDateTime.parse("2017-05-10 16:30", dtf);

print date in format YYYY-MM-DDThh:mm:ssTZD without using JODA time or Apache fast date format [duplicate]

This question already has answers here:
How to format date string in java? [duplicate]
(3 answers)
Closed 5 years ago.
Can anyone help me to print date in the format below
2016-11-22T14:52:17+05:30
without using JODA Time and apache fast date format.
Use SimpleDateFormat:
System.out.println(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").format(new Date()));
or in Java8 DateTimeFormatter:
System.out.println(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ").format(ZonedDateTime.now()));

Java - Date Format converter? [duplicate]

This question already has answers here:
java.util.Date format conversion yyyy-mm-dd to mm-dd-yyyy
(8 answers)
Closed 7 years ago.
Has java any function where I have a String with a date in this form : ddMMyy or dd.MM.yy
and I want to convert it in another format like: dd:MM:yy
I would suggest using two SimpleDateFormat objects for this.
Date date = new SimpleDateFormat("ddMMyy").parse( dateString);
String result = new SimpleDateFormat("dd:MM:yy").format( date );
For the format, see the documentation: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Formatting date string to desired date in Java [duplicate]

This question already has answers here:
How to format a date String into desirable Date format
(5 answers)
Closed 7 years ago.
I desire to format date string ex.Jul1,201512: 00: 00AM to date in format 'dd/MM/yyyy'.
If any one knows is please share the solution with me
Use a SimpleDateFormat to parse your original string and then another one to format it into the desired format.

Categories

Resources