How to Get the Date and Time

Discussion in 'Resources' started by KeybordPiano459, Oct 23, 2012.

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

    KeybordPiano459

    So I've seen someone lately that wanted to get the date and time of the server, and here I have it! First, you're going to need to define some variables.
    Code:java
    1. Date now = new Date();
    2. SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

    You can edit that however you want, so for example, you could have MM/dd/yyyy to have it print 10/23/2012 instead of 23-10-2012.

    After that, you're going to actually want to get the date and print it. In the past, I've used it to... display the date and time... I do that like this:
    Code:java
    1. System.out.println("[" + format.format(now) + "] " + message);

    The first format is the variable that you set earlier, then the second format is java's DateFormat. The message that prints something is along the lines of this:
    Code:
    [11-10-2012 15:44:11] Message
    REMEMBER: This prints the time of the computer that is running the minecraft server. So if you were to have a command like /time and send the time to a player, it wouldn't print the time of the player's computer, it would print the time of the computer that is running the server.
     
  2. Offline

    chasechocolate

    Thanks for clarifying that, I think this will help a lot of people, not necessarily while making Bukkit plugins!
     
  3. Offline

    KeybordPiano459

  4. Offline

    the_merciless

    This is great. Could you give an example of how I would get the date saved in a file, and check how long ago it was.
     
  5. Offline

    KeybordPiano459

    By using a java filewriter, and then using some method to get the time from the date you put in there. I'm not making the code for you ._.
     
  6. Offline

    the_merciless

    Lol. That's what i don't know how to do, i have saved the time and date to a file but dont know what next. Do i get it from the file as a string, and how do i compare it to todays date. I want to test if the date in the file was more than 30 days ago. I'm not expecting you to write it for me but any hints/tips would be appreciated.
     
  7. Offline

    KeybordPiano459

    Split the string with whatever you used to split up the month/date/year in the SimpleDateFormat you created, and compare those results with the results that you get with the date today.
     
    microgeek likes this.
  8. Offline

    gomeow

    You know, you could've just googled this
     
  9. Offline

    valon750

    KeybordPiano459

    Um, now.. I'm not sure if I'm doing something wrong.. or if this plugin is drunk...

    But when I had it grab the date.. I got a result of "01-01-1970"...

    valon750
    Offtopic, but dang, that was really close to 1969

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 1, 2021
  10. Offline

    the_merciless

    Not sure why you got that result. I remember reading somewhere that if you get System.currentTimeMillis() it will return the amount of milliseconds since 01.01.70

    Show us your code.
     
  11. Offline

    amhokies

    If you want to find the difference in two times, it's easier to store System.currentTimeMillis() to a file, then compare it to the current time, whenever you're checking.
     
  12. Offline

    valon750

    the_merciless

    Code:
                    Date now = new Date(0);
                    SimpleDateFormat format = new SimpleDateFormat("dd-mm-yyyy");
                    nameconfig.set("Join-Date", format.format(now));
                    nameconfig.save(name);
    Have to use "(0)" for Date, other wise it brings a NullPointerException.
     
    GrandmaJam likes this.
  13. Offline

    the_merciless

    Whats the 0 for?

    Date now = new Date(0);
    Date now = new Date();
     
  14. Offline

    valon750

    the_merciless

    Like I said, just having Date() gives me a EventException.

    Code:
    20:26:34 [SEVERE] Could not pass event PlayerMoveEvent to MitzolSurvival vv1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
        at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:227)
        at net.minecraft.server.v1_6_R3.Packet10Flying.handle(SourceFile:136)
        at net.minecraft.server.v1_6_R3.NetworkManager.b(NetworkManager.java:296)
        at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:116)
        at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
        at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:592)
        at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:227)
        at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
        at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
        at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    Caused by: java.lang.Error: Unresolved compilation problem:
        The constructor Date() is undefined
     
        at me.valon.MitzolSurvival.MitzolSurvival.onPlayerMove(MitzolSurvival.java:85)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
        ... 14 more
    
     
  15. Offline

    the_merciless

    Check your imports, you want java.util not java.sql
     
    KeybordPiano459 likes this.
  16. Offline

    valon750

    the_merciless likes this.
  17. Offline

    the_merciless

    Was just a good guess. :p
     
Thread Status:
Not open for further replies.

Share This Page