Solved MySQL Date

Discussion in 'Plugin Development' started by cfil360, Jun 7, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    cfil360

    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
     
  2. Offline

    sirantony

    Where is the 'format' code?
     
  3. Offline

    cfil360

    sirantony
    Format class
    Code:java
    1. public class DateUtil {
    2. public static DateFormat formatDate() {
    3. return new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    4. }
    5. }


    Example of format
    Code:java
    1. Date expiration = getExpiration(uuid);
    2. DateUtil.formatDate().format(expiration);
    3. Date current = Calendar.getInstance().getTime();
    4. DateUtil.formatDate().format(current);
     
  4. Offline

    sirantony

    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.
     
  5. Offline

    cfil360

    sirantony the formatting isn't the problem, because it formats correctly, its just that the date received doesn't include hours, mins, seconds
     
  6. Offline

    Garris0n

    Post the full class.
     
    cfil360 likes this.
  7. Offline

    cfil360

    Garris0n sure (Why does the forums mess up formatting?)
    Code:java
    1. package com.cfil360.AdvancedBan.Util;
    2.  
    3. import com.cfil360.AdvancedBan.AdvancedBan;
    4. import com.cfil360.AdvancedBan.Bans.Ban;
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.entity.Player;
    7.  
    8. import java.sql.Connection;
    9. import java.sql.DriverManager;
    10. import java.sql.PreparedStatement;
    11. import java.sql.ResultSet;
    12. import java.util.Calendar;
    13. import java.util.Date;
    14. import java.util.UUID;
    15.  
    16. /**
    17. * Created by connor on 6/5/2014.
    18. */
    19. public class MySQL {
    20.  
    21. private Connection connection;
    22.  
    23. public MySQL(String ip, String userName, String password, String db) {
    24. try {
    25. Class.forName("com.mysql.jdbc.Driver");
    26. connection = DriverManager.getConnection("jdbc:mysql://" + ip + "/" + db + "?user=" + userName + "&password=" + password);
    27. } catch (Exception e) {
    28. e.printStackTrace();
    29. }
    30. }
    31.  
    32. public String getModerator(UUID uuid) {
    33. try {
    34. PreparedStatement statement = connection.prepareStatement("select moderator from bans where uuid='" + uuid + "'");
    35. ResultSet result = statement.executeQuery();
    36.  
    37. if (result.next()) {
    38. String moderator = result.getString("moderator");
    39. result.close();
    40. statement.close();
    41. return moderator;
    42. } else {
    43. return null;
    44. }
    45. } catch (Exception e) {
    46. return null;
    47. }
    48. }
    49.  
    50. public java.util.Date getExpiration(UUID uuid) {
    51. try {
    52. PreparedStatement statement = connection.prepareStatement("select expiration from bans where uuid='" + uuid + "'");
    53. ResultSet result = statement.executeQuery();
    54.  
    55. if (result.next()) {
    56. java.util.Date expiration = new java.util.Date(result.getDate("expiration").getTime());
    57. result.close();
    58. statement.close();
    59. return expiration;
    60. } else {
    61. return null;
    62. }
    63. } catch (Exception e) {
    64. return null;
    65. }
    66. }
    67.  
    68. public String getReason(UUID uuid) {
    69. try {
    70. PreparedStatement statement = connection.prepareStatement("select reason from bans where uuid='" + uuid + "'");
    71. ResultSet result = statement.executeQuery();
    72.  
    73. if (result.next()) {
    74. String reason = result.getString("reason");
    75. result.close();
    76. statement.close();
    77. return reason;
    78. } else {
    79. return null;
    80. }
    81. } catch (Exception e) {
    82. return null;
    83. }
    84. }
    85.  
    86. public void insertBan(Ban ban) {
    87. try {
    88. PreparedStatement statement = connection.prepareStatement("insert into bans (uuid, moderator, expiration, reason)\nvalues ('" + ban.getUUID() + "', '" + ban.getModerator() + "', '" + DateUtil.formatDate().format(ban.getExpiration()) + "', '" + ban.getReason() + "');");
    89. statement.executeUpdate();
    90. statement.close();
    91. } catch (Exception e) {
    92. e.printStackTrace();
    93. }
    94. }
    95.  
    96. public void deleteBan(UUID uuid) {
    97. try {
    98. PreparedStatement statement = connection.prepareStatement("delete from bans where uuid='" + uuid + "'");
    99. statement.executeUpdate();
    100. statement.close();
    101. } catch (Exception e) {
    102. e.printStackTrace();
    103. }
    104. }
    105.  
    106. public boolean hasBanEnded(UUID uuid) {
    107. Date expiration = getExpiration(uuid);
    108. DateUtil.formatDate().format(expiration);
    109. Date current = Calendar.getInstance().getTime();
    110. DateUtil.formatDate().format(current);
    111.  
    112. Bukkit.broadcastMessage("Expiration " + expiration);
    113. Bukkit.broadcastMessage("Current " + current);
    114. //if the expiration is before the current date allow the player to join and delete the ban from the database
    115. /*if(expiration.equals(current)) {
    116. deleteBan(uuid);
    117. return true;
    118. }
    119. else return false;*/
    120. return true;
    121. }
    122.  
    123. public String getBanMessage(UUID uuid) {
    124. return "§4>> §c§lBANNED §4<<\n" +
    125. "§eYour access to " + "§dConnor's Server " + "§eis suspended.\n" +
    126. "§eWhy? §f" + AdvancedBan.mySQL.getReason(uuid) + ".\n" +
    127. "§eModerator? §f" + AdvancedBan.mySQL.getModerator(uuid) + " §eExipres? §f" + AdvancedBan.mySQL.getExpiration(uuid);
    128. }
    129. }

    Important Part
    Code:java
    1.  
    2. public java.util.Date getExpiration(UUID uuid) {
    3. try {
    4. PreparedStatement statement = connection.prepareStatement("select expiration from bans where uuid='" + uuid + "'");
    5. ResultSet result = statement.executeQuery();
    6.  
    7. if (result.next()) {
    8. java.util.Date expiration = new java.util.Date(result.getDate("expiration").getTime());
    9. result.close();
    10. statement.close();
    11. return expiration;
    12. } else {
    13. return null;
    14. }
    15. } catch (Exception e) {
    16. return null;
    17. }
    18. }
     
  8. Offline

    Garris0n

    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 :p

    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.
     
    NathanWolf likes this.
  9. Offline

    cfil360

    Garris0n Ok so how can i get the DateTime from the result. There is no getDateTime function.
     
  10. Offline

    Garris0n

    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.
     
Thread Status:
Not open for further replies.

Share This Page