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 4 days ago.
Improve this question
I am reading CSV file using CSVParser. In CSV file expiryDate is in formay dd-MMM-yy (18-Feb-23) and in java code i am using below formatter and is working perfectly fine in all systems, because javacode read expiryDate as "18-Feb-23" and my formatter matches this and read date .
But same code when i ran in my system ,it is reading expiry date in format "dd/MMM/yy" (18/FEB/23) [note it is read as FEB not Feb] and i have to change formatter to "dd/MMM/yy" . And in my formatter i have to call parseCaseInsensitive() because my Parser reads expiryDate as 18/FEB/23.
private static DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder().parseCaseInsensitive() .appendPattern("dd-MMM-yy").toFormatter(Locale.ENGLISH);
Problem is in my system because, in every other system when used code for CSV data 18-Feb-23 and i gave format as dd-MMM-yy in code and it worked. But for my laptop I have to give format as dd/MMM/yy.
need inputs to proceed on this further.
sample code:
try (
private static DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder().parseCaseInsensitive() .appendPattern("dd-MMM-yy").toFormatter(Locale.ENGLISH);
// input expiry date fromm CSV is "18-Feb-23"
Reader reader = Files.newBufferedReader(Paths.get(SAMPLE_CSV_FILE_PATH));
CSVParser csvParser = new CSVParser(reader, CSVFormat.EXCEL.withHeader()
.withIgnoreHeaderCase()
.withTrim());
) {
List<CSVRecord> records = csvParser.getRecords();
for (CSVRecord record : records) {
// code in my system read expiry as 18/FEB/23 but when i ran same code in other system it read as 18-Feb-23
String exp = record.get("expiry");
LocalDate expiry = null;
LocalDate expiry = null;
if (null != exp && !"".equals(exp)) {
expiry = LocalDate.parse(exp, dateTimeFormatter);
}
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 months ago.
Improve this question
How to convert string to LocalDateTime,how to solve string format "yyyymmddhhMMss" to LocalDateTime
String dateTime = "20221120000000";
LocalDateTime localDateTime = LocalDateTime.parse(dateTime);
Exception in thread "main" java.time.format.DateTimeParseException: Text '20221120000000' could not be parsed at index 0
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1948)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1850)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
at java.time.LocalDateTime.parse(LocalDateTime.java:477)
at com.company.Main.main(Main.java:21)
This should do it:
String dateTimeString = "20221120000000";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, formatter);
If you dont want to use the DateTimeFormatter, your String needs to be in ISO format
Edit: it isn’t in the question, but you said elsewhere:
I want it to convert to this format("yyyy-MM-dd HH:mm:ss") …
A LocalDateTime cannot have a format (its toString method invariably produces an ISO 8601 format). So to obtain a specific format you need to convert to a String again:
DateTimeFormatter wantedFormatFormatter
= DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
String formattedDateTime = dateTime.format(wantedFormatFormatter);
System.out.println(formattedDateTime);
This outputs:
2022-11-20 00:00:00
(I hope you didn’t expect a LocalDateTime with the format you mentioned. In case you did see this question: Can’t rid of 'T' in LocalDateTime.)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 10 months ago.
Improve this question
We have a requirement to where the input file will be KYB_FX_SPOT_AUTO.20220327 where KYB_FX_SPOT_AUTO. would be a constant and 20220327 is dynamic part which is date in format yyyyddMM, we need to assign a variable then a name AUTO_FXUPLOAD_APR2022.CSV , where other than APR2022 , rest other part is constant.
Please help me with just a piece of code to convert 20220327 name to APR2022.Please note the month in source is march and APR in target which means month has to increment by one and accordingly the year might increase in case of December month.
Thanks.
If you are using Java 8 or any later version, you can use classes from java.time.
You can convert a date String of the pattern "uuuuMMdd" to one formatted as "MMMuuuu" like this:
public static void main(String[] args) {
// your example String date
String date = "20220327";
// prepare a parser for the pattern of your input
DateTimeFormatter parser = DateTimeFormatter.ofPattern("uuuuMMdd");
// parse the String to a LocalDate using the parser
LocalDate localDate = LocalDate.parse(date, parser);
// prepare a formatter for your desired output
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMuuuu", Locale.ENGLISH);
// prepare some result message using the parser and the formatter…
String resMsg = String.format("%s ———> %s",
localDate.format(parser),
localDate.format(formatter).toUpperCase());
// … then print the message
System.out.println(resMsg);
}
The output of this is
20220327 ———> MAR2022
To create your desired result considering year changes when adding or subtracting a month, maybe use a YearMonth. Create ony by getting the year and month of the LocalDate and safely add one month to it.
Here's one option:
// extract the year month and add 1
YearMonth yearMonth = YearMonth.from(localDate).plusMonths(1);
// print the result
System.out.println("+ 1 month ==> " + yearMonth.format(formatter).toUpperCase());
Appending the above lines to the example main would add another line to the output:
+ 1 month ==> APR2022
What you are looking for is date formatting. You can use Date formats to parse the date, then output it in a different format.
The relevant classes are Date and SimpleDateFormat. For the calculation part, use Calendar.
// parse the date
String input = "20220327";
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyyMMdd");
Date myDate = inputFormat.parse(input);
// For the calculation part, refer to the javadoc of Calendar
// ...
// output in new format
SimpleDateFormat outputFormat = new SimpleDateFormat("MMMyyyy")
String output = outputFormat.format(myDate);
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 5 years ago.
Improve this question
I want date without time in java
unix format is
YYYYMMDDHHMMSS.miliseconds
unix string 20170817134131.384
string val = "20170817134131.384";
if (val != null) {
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
try {
date = df.parse(String.valueOf(val));
} catch (ParseException e) {
throw new RuntimeException("Failed to parse date: ", e);
}
return date;
}
Here's a simple solution to convert unix time to localDate and then you're free to format it as you please:
long time = 1491231860;
final LocalDate localDate = LocalDate.fromMillisSinceEpoch(unixSeconds);
I am trying to retrieve the changeset number of a file, so I can place within a comment block at the top of the file.
I have already retrieved other details such as comments, author and date and time by the code below.
Code
String comments = pc.getPendingChanges().getComment();
String author = pc.getPendingChanges().getAllPendingChanges()[0].getPendingSetOwnerDisplay();
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
PC is:
final PendingCheckin pc = getPendingCheckin();
Edit
Any tips on how to improve the question?
String version = String.valueOf(pc.getPendingChanges().getAllPendingChanges()[0].getVersion());
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 9 years ago.
Improve this question
I have a String stating the date as "2012-12-31" how can I convert it into date format using java and store it MySQL database where the type in MySQL is of date type.
You can do as follows
String string = "2012-12-31";
DateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
java.util.Date date = formatter.parse(string);
java.sql.Date sqlDate = new java.sql.Date(date.getTime()); // convert java.util.date to java.sql.date
Have a look at SimpleDateFormat
String string = "2012-12-31"; //You have like this now
DateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
Date date = formatter.parse(string);
System.out.println(date);