LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-01-2023, 01:06 PM   #1
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,204

Rep: Reputation: 165Reputation: 165
SimpleDateFormat apparently not working for mySQL


I have a mySql table column defined as datetime. I'm trying to display this in my java program using:
Code:
SimpleDateFormat mmddyyyy = new SimpleDateFormat("MM/dd/yyyy h:mm a");
bidDate = rs.getDate("bidDate");

System.out.println(mmddyyyy.format(bidDate));
The actual database date is 2022-12-19 14:38, but the above displays as "12/19/2022 12:00 A". If I remove the mmddyyy format and display the raw database value 'System.out.println(bidDate);' it shows only "2022-12-19", no time component. If I do rs.getString("bidDate"), the String is displayed as 2022-12-19 14:38:00.

I've been showing MS SQLserver datetimes this way for over 10 years w/o problem. What needs to be done differently with MySql?

Last edited by mfoley; 01-01-2023 at 01:07 PM.
 
Old 01-01-2023, 05:55 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 25,512

Rep: Reputation: 7625Reputation: 7625Reputation: 7625Reputation: 7625Reputation: 7625Reputation: 7625Reputation: 7625Reputation: 7625Reputation: 7625Reputation: 7625Reputation: 7625
Quote:
Originally Posted by mfoley View Post
I have a mySql table column defined as datetime. I'm trying to display this in my java program using:
Code:
SimpleDateFormat mmddyyyy = new SimpleDateFormat("MM/dd/yyyy h:mm a");
bidDate = rs.getDate("bidDate");
System.out.println(mmddyyyy.format(bidDate));
The actual database date is 2022-12-19 14:38, but the above displays as "12/19/2022 12:00 A". If I remove the mmddyyy format and display the raw database value 'System.out.println(bidDate);' it shows only "2022-12-19", no time component. If I do rs.getString("bidDate"), the String is displayed as 2022-12-19 14:38:00. I've been showing MS SQLserver datetimes this way for over 10 years w/o problem. What needs to be done differently with MySql?
I typically use TIMESTAMP if I need the date and time (with seconds/etc.), instead of datetime. Might be worth a try to modify the table and try it, if you're just starting with this project.
 
Old 01-01-2023, 11:19 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,426
Blog Entries: 1

Rep: Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679
Try this:
Code:
bidDate = rs.getTimestamp("bidDate");
 
1 members found this post helpful.
Old 01-24-2023, 12:26 PM   #4
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,204

Original Poster
Rep: Reputation: 165Reputation: 165
TB0ne: I've long used datetime on SQL Server and rs.getDate() to retrieve the value in Java. However, using NevemTeve's suggested rs.getTimestamp() makes sense. getDate() would imply that your just getting the date, even though with SQL Server you do get date and time. getTimestamp() implies both date and time.

NevemTeve's suggestion worked. I think I'll leave the database datatype as datetime since all other such tables with datestamps are that way.
 
  


Reply

Tags
date, format, java, mysql


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
/etc/cron.daily/apt apparently not working doru Linux - Distributions 9 08-31-2010 02:28 AM
MySQL -apparently- will not in Debian Etch walterbyrd Linux - Software 10 02-07-2010 08:24 PM
Issues with getaddrinfo() apparently not working as expected. geustace Linux - Networking 1 01-19-2010 02:54 PM
chmod apparently not working Noido Linux - Software 2 06-29-2005 02:32 PM
Apparently working NICs receiving but not sending CestusGW Linux - Networking 8 11-02-2003 02:28 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration