Date

Discussion in 'Plugin Development' started by ItsBlockFighter, May 1, 2017.

Thread Status:
Not open for further replies.
  1. Hi!
    How do I make it possible to add +30 days to the polling day?
    Code:
    lore.add(ChatColor.GRAY + "Expiry: " + ChatColor.GREEN + ???);
     
  2. Online

    timtower Administrator Administrator Moderator

  3. So?
    lore.add(ChatColor.GRAY + "Expiry: " + ChatColor.GREEN + Calendar.getInstance().add(Calendar.DATE, 30));
    Because she does not want to be good
     
  4. Online

    timtower Administrator Administrator Moderator

    @ItsBlockFighter You need to use multiple lines of code.
    This is not a single line thing.
    You need to get the instance.
    Add 30 days.
    Format it to a readable string.
    Put it in the lore.
     
  5. Code:
    Date now2 = new Date();
                    Calendar c = Calendar.getInstance();
                    c.setTime(now2);
                    c.add(Calendar.DATE, 30);
                    now2 = c.getTime();
    That's good? And then how can I add a
    Code:
    lore.add(ChatColor.GRAY + "Expiry: " + ChatColor.GREEN + ???);
    Or do you still need a SimpleDateFormat?
     
  6. Online

    timtower Administrator Administrator Moderator

  7. Did you think of this?
    Code:
    SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
     
  8. Offline

    DoggyCode™

    Code:java
    1. Calendar now = Calendar.getInstance();
    2. System.out.println(now.getTime().toString());
    3.  
    4. // Adding 30 days
    5. now.add(Calendar.DATE, 30);
    6.  
    7. Date then = now.getTime();
    8. System.out.println(then);
    9.  
    10. SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
    11.  
    12. String formatted = sdf.format(then);
    13. System.out.println(formatted);


    Add the formatted string to the lore :)

    EDIT:
    Output:
    Code:
    Tue May 02 12:10:45 CEST 2017
    Thu Jun 01 12:10:45 CEST 2017
    01-06-2017 12:10:45
    Should be accurate :)
     
  9. @DoggyCode™
    I did it but not good
    Code:
    alendar now2 = Calendar.getInstance();
                    System.out.println(now2.getTime().toString());
                    
                    now2.add(Calendar.DATE, 30);
                    
                    Date then = now2.getTime();
                    System.out.println(then);
                    
                    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
                    
                    String formatted = sdf.format(then);
                    System.out.println(formatted);
    Lore:
    Code:
    lore.add(ChatColor.GRAY + "Expiry: " + ChatColor.GREEN + sdf.format(now2));;
     
  10. Offline

    DoggyCode™

    Weird. Full code?


    Sent from my iPhone using Tapatalk
     
  11. @DoggyCode™
    Full Code:
    Code:
    package me.risegaming.RG_Igazolvany.Commands;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.Date;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemFlag;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    import me.risegaming.RG_Igazolvany.Main.Errors;
    import me.risegaming.RG_Igazolvany.Main.Permissions;
    public class Fegyverengedely implements CommandExecutor {
      
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            String command = cmd.getName();
            if (command.equalsIgnoreCase("fegyverengedely")) {
                if (sender instanceof Player) {
                    Player player = (Player) sender;
                    Location loc = player.getLocation();
                    Date now = new Date();
                    SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
                   
                    Calendar now2 = Calendar.getInstance();
                    System.out.println(now2.getTime().toString());
                    
                    now2.add(Calendar.DATE, 30);
                    
                    Date then = now2.getTime();
                    System.out.println(then);
                    
                    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
                    
                    String formatted = sdf.format(then);
                    System.out.println(formatted);
                    if (player.hasPermission(Permissions.getPermission(Permissions.COMMAND_SZEMELYI))) {
                        if ((int)loc.getX() == 2568 && (int)loc.getY() == 83 && (int)loc.getZ() == 1475) {
                            if (args.length == 0) {
                                player.sendMessage(ChatColor.RED + "[]==========[" + ChatColor.GREEN + "Fegyverengedly igazolvány" + ChatColor.RED + "]==========[]");
                                player.sendMessage(ChatColor.WHITE + "/fegyverengedely lekérés" + ChatColor.GRAY + " Fegyverengedély lekérés");
                                player.sendMessage(ChatColor.RED + "[]==========[" + ChatColor.GREEN + "Igazolvány System" + ChatColor.RED + "]==========[]");
                            } else if(args.length == 1) {
                                if(args[0].equalsIgnoreCase("lekérés")) {
                                    ItemStack utlevel = new ItemStack(Material.PAPER);
                                    player.sendMessage(ChatColor.GRAY + "Ghost" + ChatColor.GOLD + "Realms " + ChatColor.DARK_GRAY + " »" + ChatColor.GREEN + "Sikeresen megvetted a fegyverengedélyt!");
                                    ItemMeta meta = utlevel.getItemMeta();
                                    meta.setDisplayName(ChatColor.GREEN + ChatColor.BOLD.toString() +  "Fegyverengedély");
                                    meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
                                    ArrayList<String> lore = new ArrayList<>();
                                    lore.add(ChatColor.GRAY + "Tulajdonos: " + ChatColor.GREEN + player.getName());
                                    lore.add(ChatColor.GRAY + "Lejárat: " + ChatColor.GREEN + sdf.format(now2));;
                                    lore.add(ChatColor.GRAY + "Készült: " + ChatColor.GREEN + format.format(now));
                                    meta.setLore(lore);
                                    utlevel.setItemMeta(meta);
                                    player.getInventory().addItem(utlevel);
                                    }
                                }
                        }
                    } else {
                        player.sendMessage(ChatColor.RED + Errors.getErrorMessage(Errors.NO_PERMISSIONS));
                    }
                } else if (sender instanceof ConsoleCommandSender) {
                    Bukkit.getConsoleSender()
                            .sendMessage(ChatColor.RED + Errors.getErrorMessage(Errors.PLAYER_ONLY_COMMAND));
                }
            }
            return true;
        }
    }
     
  12. Offline

    DoggyCode™

    Because you are formatting this object:
    PHP:
    Date now = new Date();
    Which does not have the added 30 days. Anyways, you have a String there called formatted. Just use that one.
    PHP:
    lore.add(ChatColor.GRAY "Készült: " ChatColor.GREEN formatted);
     
  13. Offline

    Caderape2

  14. Offline

    DoggyCode™

Thread Status:
Not open for further replies.

Share This Page