tg95 Posted December 22, 2015 at 09:16 PM Report Share #590982 Posted December 22, 2015 at 09:16 PM Boas a todos, eu estou com uns problemas num codigo java, eu estou a tentar fazer um timer em currentTimeMillis, ja consegui saber os segundos, minutos e horas, mas preciso de saber o dia, mes e ano, deixo o codigo abaixo. ja tentei porrada de coisas ja procurei e ainda nao consegui saber o dia,mes e ano. long mill = System.currentTimeMillis(); long seconds = (mill / 1000) % 60 ; long minutes = ((mill / (1000*60)) % 60); long hours = ((mill / (1000*60*60)) % 24); System.out.println(hours+" "+minutes+" "+seconds+" "); obrigado pela atençao Link to comment Share on other sites More sharing options...
Hercles Posted December 22, 2015 at 11:08 PM Report Share #590994 Posted December 22, 2015 at 11:08 PM É pra saber a data atual com a hora, minuto e segundo??? tenta isto: import java.text.SimpleDateFormat; import java.util.Date; public class Dataatual { public static void main(String[] args) { SimpleDateFormat formatador = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); Date data = new Date(System.currentTimeMillis()); System.out.println(formatador.format(data)); } } Link to comment Share on other sites More sharing options...
tg95 Posted December 23, 2015 at 04:51 PM Author Report Share #591011 Posted December 23, 2015 at 04:51 PM (edited) Boas colega, esse codigo que mandou nao e bem o que eu preciso eu preciso a continuação do código que mostrei, pois vou precisar de fazer algo dentro deste genero: int time = config.getInt("time"); long hours = ((time/ (1000*60*60)) % 24); if(hours >= 0) { /*code*/ } so que preciso de fazer com secs,mins,hours,days,months,years, obrigado pela atenção Edited December 23, 2015 at 04:52 PM by tg95 Link to comment Share on other sites More sharing options...
Hercles Posted December 27, 2015 at 09:13 PM Report Share #591079 Posted December 27, 2015 at 09:13 PM (edited) Não sei se entendi... Tenta fazer isto usando o Calendar public class Dataatual { public static void main(String[] args) { long mill = System.currentTimeMillis(); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(mill); int ano = calendar.get(Calendar.YEAR); int mes = calendar.get(Calendar.MONTH); int semana = calendar.get(Calendar.WEEK_OF_MONTH); int dia = calendar.get(Calendar.DAY_OF_MONTH); int hora = calendar.get(Calendar.HOUR_OF_DAY); int minuto = calendar.get(Calendar.MINUTE); int segundo = calendar.get(Calendar.SECOND); // mes, soma-se mais 1, porque começa em zero. System.out.println(dia + " " + (mes + 1) + " " + ano + " " + hora + " " + minuto + " " + segundo); } } Edited December 27, 2015 at 09:15 PM by Hercles Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now