This question already has answers here:
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
How to convert date in to yyyy-MM-dd Format?
(6 answers)
Closed 2 years ago.
I have a
String s = "2020-02-22"`;
and I want to change it to Date so I can store it in my database which has has a column that does not accept anything but Date.
I tried using the LocalDate class but it's in API 26.
Any help would be appreciated.
Assume that you fetch the date from database and pass it to the below method:
public String formatDate(Date date){
SimpleDateFormat ff = new SimpleDateFormat("yyyy-MM-dd");
return ff.format(date);
}
EDIT : based on input from Basil, you could try Android Desugaring to make use of Java 8+ functionality without the need of minimum API level. This would allow the use of LocalDate instead of the old java.util.Date class.
Using LocalDate you could parse a string to date as:
public LocalDate getDate(String dateString) {
return LocalDate.parse(dateString);
}
Related
This question already has answers here:
How to format LocalDate object to MM/dd/yyyy and have format persist
(4 answers)
Unable to Convert String to localDate with custom pattern [duplicate]
(1 answer)
Closed 1 year ago.
I am trying to parse the date in a specific pattern but every time it is showing date in only format. Here what I am doing. Output is written in comments in front of the line.
String dat = "20-06-2021";
String newFormatLocaleDate = "MMM dd yyyy";
String newFormattedDate = convertToNewFormat(dat,currentFormat,newFormatLocaleDate); //Jun 20 2021
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern(newFormatLocaleDate);
LocalDate parse1 = java.time.LocalDate.parse(newFormattedDate,formatter1); //2021-06-20
System.out.println("Using parse1 Pattern Only "+parse1); //2021-06-20
Date date1 = java.sql.Date.valueOf(parse1);
System.out.println("Using Date Pattern Only "+date1); //2021-06-20
The pattern in which date is required is given and in the string form it giving the correct value but when I am trying to parse it with LocaleDate it is changing the format for all dates to the above mentioned (yyyy-MM-dd) format irrespective of the newFormatLocaleDate pattern.
Thanks
You should not expect the line
System.out.println("Using parse1 Pattern Only "+parse1);
to output something different than 2021-06-20 it literally is exactly what the docs say what will be out putted.
The output will be in the ISO-8601 format uuuu-MM-dd.
So regardless of the format you used while parsing the LocalDate value the toString method will always output a data in the format uuuu-MM-dd.
If you want a different format use the instance method format.
Please note that you are printing the LocalDate and Date instances (using their toString() implementation), therefore not formatted according to your desired template. If you want to print out your parsed date in a particular format, you need to apply a formatter again, for example like this:
System.out.println(parse1.format(DateTimeFormatter.ofPattern("MMM dd yyyy")))
This question already has answers here:
How can I Convert Calendar.toString() into date using SimpleDateFormat.parse()?
(2 answers)
Closed 2 years ago.
I am building a API Java code that is downloading into a CSV file a list of transactions, disputes and payments made through Paypal for this company I work for. One of the columns from the CSV file is a date related column as you can see below:
transactionData.add(String.valueOf(transaction.getDisputes().get(i).getReceivedDate()));
The issue is that all values for the data column above is coming in the CSV as a XMLGregorianDate:
java.util.GregorianCalendar[time=1589760000000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2020,MONTH=4,WEEK_OF_YEAR=21,WEEK_OF_MONTH=3,DAY_OF_MONTH=18,DAY_OF_YEAR=139,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=3,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=0,DST_OFFSET=0]
What changes should I make to the line above to give me the data in timestamp with timezone i.e. "yyyy-mm-dd hh:mi:ss+/-tz"?
You have two options to specify the date-time output format:
1- Using Java 8 Date and Time API classes under the java.time package (recommended)
final GregorianCalendar gregorianCalendar = transaction.getDisputes().get(i).getReceivedDate();
final String dateTimePattern = "yyyy-MM-dd HH:mm:ss.SSSZ";
ZonedDateTime zonedDateTime = gregorianCalendar.toZonedDateTime();
final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(dateTimePattern);
final String dateFormat1 = dateFormatter.format(zonedDateTime);
transactionData.add(dateFormat1);
2- Using the legacy Date-Time classes such as java.util.Date & java.text.SimpleDateFormat.
final Date receivedDate = gregorianCalendar.getTime();
final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateTimePattern);
final String dateFormat2 = simpleDateFormat.format(receivedDate);
transactionData.add(dateFormat2);
GregorianCalendar extends Calendar that has a getTime():Date method : https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#getTime()
This question already has answers here:
display Java.util.Date in a specific format
(11 answers)
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
Closed 2 years ago.
Im getting the following error when I try to convert the following string. Id like the Date to be in the format yyyy-MM-dd'T'HH:mm:ss:SSS but instead the Date seems to be coming out as Sun Mar 01 23:00:01 GMT 2020
String FULL_ISO_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";
SimpleDateFormat formatter = new SimpleDateFormat(FULL_ISO_DATE_FORMAT);
Date from = formatter.parse("2020-03-01T23:00:01.000");
Error
feign.FeignException: status 400 reading Controller#searchController(Date,Date,Integer,String); content:
{"status":"fail","data":{"errors":[{"type":"IllegalArgumentException","description":"Invalid value Sun Mar 01 23:00:01 GMT 2020 for filter from. Field needs to be in format: yyyy-MM-dd'T'HH:mm:ss.SSS"}]}]}
Any help would be appreciated. I need to use the Date object as the constructor Im querying is using the Date object. Ideally I'd like to use LocalDateTime but I cant.
Use the LocalDateTime from java-8 date-time API and stop using legacy Date classes
String FULL_ISO_DATE_FORMAT = DateTimeFormatter. ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
LocalDateTime dateTime = LocalDateTime.now();
dateTime.format(FULL_ISO_DATE_FORMAT);
Please don't use the old classes Date and SimpleDateFormat. Use the new java.time api that is much more robust and better designed.
You can do the same thing as follows:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
LocalDateTime date = LocalDateTime.parse("2020-03-01T23:00:01.000", formatter);
Keep in mind that you can convert it to Date for compatibility like so:
Date legacyDate = Date.from(date.atZone(ZoneId.systemDefault()).toInstant());
This question already has answers here:
How do I change date time format in Android?
(11 answers)
Closed 6 years ago.
I receive DATETIME from mysql database in a format like this 2016-10-12 18:52:00. Is there an easy way to print it in a readable like 12 October 2016 6:52 pm in java or android?
You can do things like:
SimpleDateFormat simpleDate = new SimpleDateFormat("MM/DD/YYYY HH:mm:ss");
Date date = new Date();
String S = simpleDate.format(date); //This will give you like 11/20/2016 08:45:02
Now use this form and get the month name programatically according to your need.
This question already has answers here:
How can I change the date format in Java? [duplicate]
(10 answers)
Closed 7 years ago.
I hava java program , the program get table 'Employees' from oracle Database 11G
the problem is ,
java program get the Hire_Date Column in this format “YYYY-MM-DD 00:00:00.0”
I want get the date in this format “YYYY-MM-DD"
How can I solve This Problem ?
the photo will explian the problem
http://i.imgur.com/4CT7MnG.png
You can do this as part of your select statement:
SELECT TO_CHAR(HIRE_DATE, 'DD-MON-YYYY') HIRE_DATE_TRUNC FROM EMPLOYEES;
Use of to_char to format dates is described here. Alternatively, you could use a SimpleDateFormat to do the same thing in Java after the data has been extracted:
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd");
Date d = new Date();
String formatted = sdf.format(d);
System.out.println(formatted);