-*- TimeFormatter -*-It kinda gets boring just looking at an amount of seconds. You have to calculate how many minutes there are from those seconds, and stuff like that. Well, this TimeFormatter will help you organize the look of seconds. It will calculate how many milliseconds are in years, minutes, days, etc. If you still don't understand, for example, it will convert 70 seconds (or 70,000 millis) into 1 minute, and 10 seconds. DependencyIn order to use this plugin, you probably want to add BlockheadsAPI into the dependencies of your plugin, so people don't end up getting errors about a plugin not existing. plugin.ym name: TimeFormatting Creating a New TimeFormatterTo format an amount of seconds or milliseconds, this is what you would do: TimeFormatter formatter = new TimeFormatter(milliseconds); You are basically setting a long here. So if you are getting an error, make sure you cast a long to it, or if it is a number, just add an L to the end. FormattingThere are 2 ways to format your milliseconds: The replacing format (returns String): formatter.getInFormat("%Y yrs, %M months, %W weeks, %D days, %H hours, %m minutes, This formats the milliseconds into this format, replacing the variables with the numbers. The problem with this format is that if the milliseconds is not over 1 year, it will say "0 yrs." For example, if you had 70000 milliseconds, it would say (according to the format set above): 0 yrs, 0 months, 0 weeks, 0 days, 0 hours, 1 minutes, and 10 seconds. You probably don't want this, but it is there incase you do. The better format (returns String): <------- RECOMMENDED formatter.getInBetterFormat("%y years, ", "%m months, ", "%w weeks, ", "%d days, ", The thing about this format is, firstly, that the arguments are split up. In the first formatting way, it is just 1 long string, replacing the variables with the amount. This better format is split up into these sections. The next important thing about this is that it does NOT show the values that have 0! If I have 70,000 milliseconds, it will output: 1 minutes, and 10 seconds (as set in the format above). This is probably the way you would want to do it, so you don't have to see all the zeros. Getting Values SeparatelyIf you don't like any of the formats above, you could always create the format yourself. You can get each value through: formatter.getMinutes(); returns int IMPORTANT NOTE ABOUT THESE VALUES: These values are not saying how many minutes are in those milliseconds. These are saying how many minutes are LEFT. Suppose you put in 3670000 milliseconds into the formatter. That is an hour and 70 seconds. It will not say how many minutes there are in the milliseconds (it will not say 61 minutes when it returns). It will instead say 1 minute, because that is how much is left AFTER the hours are taken away. So if you use #getHours(), it will return 1 hour. Get What You Put InYou can simply get the amount of milliseconds you put in by using: formatter.getAllMillis(); returns long That's it!Leave a comment if you like it! Post suggestions or report errors by clicking buttons on the sidebar!
0 Comments
Leave a Reply. |
Download Blockhead's APISuggest a FeatureReport Errors-*- Features -*- |