LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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, 12:06 PM   #1
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,616

Rep: Reputation: 180Reputation: 180
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 12:07 PM.
 
Old 01-01-2023, 04:55 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,766

Rep: Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983
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, 10:19 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,882
Blog Entries: 1

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

Original Poster
Rep: Reputation: 180Reputation: 180
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



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 01:28 AM
MySQL -apparently- will not in Debian Etch walterbyrd Linux - Software 10 02-07-2010 07:24 PM
Issues with getaddrinfo() apparently not working as expected. geustace Linux - Networking 1 01-19-2010 01:54 PM
chmod apparently not working Noido Linux - Software 2 06-29-2005 01:32 PM
Apparently working NICs receiving but not sending CestusGW Linux - Networking 8 11-02-2003 01:28 PM

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

All times are GMT -5. The time now is 03:55 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