This might not be the correct place, but i am hoping you guys can help. I am attempting to retrieve a date from a MySQL database. When i retrieve the date i format it and then output the date. The problem is that it isn't retrieving the entire date. Date in mysql database: 2014-06-07 12:25:54 Date when outputted in java: 2014-06-07 00:00:00 thanks
sirantony Format class Code:java public class DateUtil { public static DateFormat formatDate() { return new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); }} Example of format Code:java Date expiration = getExpiration(uuid); DateUtil.formatDate().format(expiration);Date current = Calendar.getInstance().getTime(); DateUtil.formatDate().format(current);
I writed software that stores times to mysql, but I kinda lost it. But I think you can get the date from sql with a string, and then format that string.
sirantony the formatting isn't the problem, because it formats correctly, its just that the date received doesn't include hours, mins, seconds
Garris0n sure (Why does the forums mess up formatting?) Code:java package com.cfil360.AdvancedBan.Util; import com.cfil360.AdvancedBan.AdvancedBan;import com.cfil360.AdvancedBan.Bans.Ban;import org.bukkit.Bukkit;import org.bukkit.entity.Player; import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.Calendar;import java.util.Date;import java.util.UUID; /*** Created by connor on 6/5/2014.*/public class MySQL { private Connection connection; public MySQL(String ip, String userName, String password, String db) {try {Class.forName("com.mysql.jdbc.Driver");connection = DriverManager.getConnection("jdbc:mysql://" + ip + "/" + db + "?user=" + userName + "&password=" + password);} catch (Exception e) {e.printStackTrace();}} public String getModerator(UUID uuid) {try {PreparedStatement statement = connection.prepareStatement("select moderator from bans where uuid='" + uuid + "'");ResultSet result = statement.executeQuery(); if (result.next()) {String moderator = result.getString("moderator");result.close();statement.close();return moderator;} else {return null;}} catch (Exception e) {return null;}} public java.util.Date getExpiration(UUID uuid) {try {PreparedStatement statement = connection.prepareStatement("select expiration from bans where uuid='" + uuid + "'");ResultSet result = statement.executeQuery(); if (result.next()) {java.util.Date expiration = new java.util.Date(result.getDate("expiration").getTime());result.close();statement.close();return expiration;} else {return null;}} catch (Exception e) {return null;}} public String getReason(UUID uuid) {try {PreparedStatement statement = connection.prepareStatement("select reason from bans where uuid='" + uuid + "'");ResultSet result = statement.executeQuery(); if (result.next()) {String reason = result.getString("reason");result.close();statement.close();return reason;} else {return null;}} catch (Exception e) {return null;}} public void insertBan(Ban ban) {try {PreparedStatement statement = connection.prepareStatement("insert into bans (uuid, moderator, expiration, reason)\nvalues ('" + ban.getUUID() + "', '" + ban.getModerator() + "', '" + DateUtil.formatDate().format(ban.getExpiration()) + "', '" + ban.getReason() + "');");statement.executeUpdate();statement.close();} catch (Exception e) {e.printStackTrace();}} public void deleteBan(UUID uuid) {try {PreparedStatement statement = connection.prepareStatement("delete from bans where uuid='" + uuid + "'");statement.executeUpdate();statement.close();} catch (Exception e) {e.printStackTrace();}} public boolean hasBanEnded(UUID uuid) {Date expiration = getExpiration(uuid);DateUtil.formatDate().format(expiration);Date current = Calendar.getInstance().getTime();DateUtil.formatDate().format(current); Bukkit.broadcastMessage("Expiration " + expiration);Bukkit.broadcastMessage("Current " + current);//if the expiration is before the current date allow the player to join and delete the ban from the database/*if(expiration.equals(current)) {deleteBan(uuid);return true;}else return false;*/return true;} public String getBanMessage(UUID uuid) {return "§4>> §c§lBANNED §4<<\n" +"§eYour access to " + "§dConnor's Server " + "§eis suspended.\n" +"§eWhy? §f" + AdvancedBan.mySQL.getReason(uuid) + ".\n" +"§eModerator? §f" + AdvancedBan.mySQL.getModerator(uuid) + " §eExipres? §f" + AdvancedBan.mySQL.getExpiration(uuid);}} Important Part Code:java public java.util.Date getExpiration(UUID uuid) {try {PreparedStatement statement = connection.prepareStatement("select expiration from bans where uuid='" + uuid + "'");ResultSet result = statement.executeQuery(); if (result.next()) {java.util.Date expiration = new java.util.Date(result.getDate("expiration").getTime());result.close();statement.close();return expiration;} else {return null;}} catch (Exception e) {return null;}}
It messes up when you edit. You can always use sites like pastebin.com or (preferably) gist.github.com. Anyway, first of all, you're not setting the formatted date equal to anything. Calling .format() on it returns a formatted version as a String. Also, what's with this part: Code: new java.util.Date(result.getDate("expiration").getTime()); You can just use the date it returns Also, the MySQL date type doesn't actually save a time. There's a DateTime and a Timestamp. I'd recommend reading up on what each one does here.
I'm pretty sure it's just getDate() in Java regardless of the MySQL types. Also, I'd recommend using Timestamp. It takes care of the whole timezone issue rather seamlessly.