Jump to content
amvm

Converter String para Date

Recommended Posts

amvm

Boas,

estou com um problema.  Pretendo converter uma String para Date, adicionar 10 dias, e voltar a converter para String. Problema é que não me esta a dar direito, principalmente na parte de adicionar os tais 10 dias

Date date;
        int dezdias = 864000000;
        try {
            DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
            date = (java.util.Date) formatter.parse(data);
//               date.setDate(date.getDate()+dezdias);
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);

         cal.add(Calendar.DATE, 10);

            String datalimite = formatter.format(date);

Por exemplo: meto isto -> 10-10-2015

sai-me isto -> 06-06-236756 :nono1:

alguma ajuda?

Cumprimentos.

Share this post


Link to post
Share on other sites
josevellozo

converte primeiro a data para milissegundos e depois soma-lhe os dez dias. Assim funciona:

 Date date;
        int dezdias = 864000000;
        try {
            DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
            date = (java.util.Date) formatter.parse("12-05-2011");
               //date.setDate(date.getDate()+dezdias);
            long dataMili=date.getTime();
            
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            date.setTime(dataMili+dezdias);
         //cal.add(Calendar.DATE, 10);

            String datalimite = formatter.format(date);
             System.out.println(datalimite);
        }catch (Exception e){
            
            System.out.println(e.toString());
        }

Espero ter ajudado  :)

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.