• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java doesn't pick up seconds from a date string from a CSV in Excel

 
Ranch Hand
Posts: 74
  • Number of slices to send:
    Optional 'thank-you' note:


In my CSV file, I have a column whereby I enter

2024-05-05 08:15:00



If I enter that into the CSV file in Notepad, the  value of str is

2024-05-05 08:15:00

, and the second condition is met:



However, if I enter

2024-05-05 08:15:00

into the CSV file in Excel, the value of str is

5/5/2024 8:15

, and the thrid condition an't be met, because the seconds are lobbed off by the time it gets from the Excel CSV to Java and throws a java.text.ParseException:


If edited and saved by Excel: 5/5/2024 8:15
java.text.ParseException: Unparseable date: "5/5/2024 8:15"
at java.base/java.text.DateFormat.parse(DateFormat.java:396)
at com.dandylabs.FeedbackBannedIPBeanPopulator.populateBean(FeedbackBannedIPBeanPopulator.java:104)
at com.dandylabs.FeedbackBannedIPUpdater.readCSV(FeedbackBannedIPUpdater.java:282)
at com.dandylabs.FeedbackBannedIPUpdater.perform(FeedbackBannedIPUpdater.java:264)
at com.dandylabs.FeedbackBannedIPUpdater.main(FeedbackBannedIPUpdater.java:307)



I don't know how to fix this, considering users will be editing their CSV files by either Excel or Notepad, depending on who they are, etc.  I need for the expire date to be consistently formatted in such a way as to retain seconds every single time, but if they edit in Excel, the seconds are lost by the time Java gets a hold of it.

Any ideas? I am open (in this particular project) to external libraries if need be, e.g. Apache POI, etc.

Thanks
 
Saloon Keeper
Posts: 10796
86
  • Number of slices to send:
    Optional 'thank-you' note:
In Excel, what is the FORMAT of the date/time cell?
 
Phillip Powell
Ranch Hand
Posts: 74
  • Number of slices to send:
    Optional 'thank-you' note:

Carey Brown wrote:In Excel, what is the FORMAT of the date/time cell?



The cell says

5/5/2024 08:15:00 AM

 
Carey Brown
Saloon Keeper
Posts: 10796
86
  • Number of slices to send:
    Optional 'thank-you' note:

Phillip Powell wrote:

Your code snippet is incomplete. You are not using MDY_HMS_SQL_FULL_SDF for instance. We don't know what's in StringUtilities or DateUtilities.

In order for people to help you quickly it's best to provide an SSCCE (Simple Self Contained Compilable Example) that we can download and execute ourselves. An SSCCE is boiled down to only those lines of code necessary to demonstrate the issue and the code should not depend on external code that has not been provided. If possible, external code should be expanded in place.
 
Carey Brown
Saloon Keeper
Posts: 10796
86
  • Number of slices to send:
    Optional 'thank-you' note:
You can set a custom format in Excel to "m/d/yyyy h:mm:ss" that will not truncate seconds.
 
Phillip Powell
Ranch Hand
Posts: 74
  • Number of slices to send:
    Optional 'thank-you' note:

Carey Brown wrote:You can set a custom format in Excel to "m/d/yyyy h:mm:ss" that will not truncate seconds.



I wound up with a workaround due to a requirement change: Seconds not needed, so never mind about lobbing off seconds.

I was able to get it to successfully parse in both Notepad and Excel:

 
Carey Brown
Saloon Keeper
Posts: 10796
86
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:
Something like this:
 
Carey Brown
Saloon Keeper
Posts: 10796
86
  • Number of slices to send:
    Optional 'thank-you' note:
Some tweaks I had added.
 
Phillip Powell
Ranch Hand
Posts: 74
  • Number of slices to send:
    Optional 'thank-you' note:
Due to my personal mental limitations, this will take an extremely long time to digest, but thanks for helping!
 
Carey Brown
Saloon Keeper
Posts: 10796
86
  • Number of slices to send:
    Optional 'thank-you' note:
Let me know if you have any questions.
 
Ranch Foreman
Posts: 911
8
  • Number of slices to send:
    Optional 'thank-you' note:
Phillip and Carey,

Great question and answers.  I don't understand the entire solution.  I"m learning a lot from reading it.   I'm guessing at what I think its doing.  I added comments to the code.  

 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Number of slices to send:
    Optional 'thank-you' note:
I hope it was OK that I I jumped into the discussion.  I hope I didn't hi jack anything.

Thanks,

Kevin
 
Carey Brown
Saloon Keeper
Posts: 10796
86
  • Number of slices to send:
    Optional 'thank-you' note:
Yes, setTimestamp( ... ) is overloaded, there are three of them and the compiler knows which one to choose based on the arguments being passed in. They all result in the field "timestamp" being set.
 
Carey Brown
Saloon Keeper
Posts: 10796
86
  • Number of slices to send:
    Optional 'thank-you' note:
There is a chain in the way the setTimestamp(...) methods are written.

setTimestamp( String )
calls setTimestamp( SimpleDateFormat, String )
which calls setTimestamp( Timestamp )

Calling any one of them results in the last one being called which does the final setting of the field.

The first one was not originally named setTimestamp(..) but seeing as how that is what it does but using a String argument instead, it makes sense to name it the same. The second one is called by setTimestamp(String) in two places so it eliminates some code redundancy.
 
Carey Brown
Saloon Keeper
Posts: 10796
86
  • Number of slices to send:
    Optional 'thank-you' note:
@Kevin - please keep line lengths short so that they don't get truncated  when being displayed. Take as many lines as you need.
 
Marshal
Posts: 79422
377
  • Number of slices to send:
    Optional 'thank-you' note:

Carey Brown wrote:. . . please keep line lengths short . . .

. . . and please reserve // comments for things very short. Your long comments should be broken into multi‑line /* comments */
 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Number of slices to send:
    Optional 'thank-you' note:
Carey and Campbell,

I didn't know my comments would be truncated.  Now I"m aware of it.

I'll use the /*           */    comment style          

Thank you,

Kevin
reply
    Bookmark Topic Watch Topic
  • New Topic
vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |