Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

194
Visualizações
How to parse String to Date object?

I'm trying to create a date variable based on a date I have chosen. When I use the the simpledateformat to parse it to the date object, it will not work. Please can someone help me?

package com.example.rossr.tlatimetableweek;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import java.util.*;
import java.text.*;

public class A_Week extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_a__week);

    DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    Date currentDate = new Date();

    Log.i("Current Date",dateFormat.format(currentDate));

    Date termDate = dateFormat.parse("11/12/2017"); //error on this line
}
}
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Try this

   try {

      SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
       Date date = dateFormat.parse("11/12/2017")
     }
    catch (ParseException ex){
        Log.i("Current Date","error occurred while parsing date");
     }
over 4 years ago · Santiago Trujillo Relatório

0

This is because DateFormat.parse method throws ParseException and you need to handle it. declare it in the method signature or handle the exception with try and catch block. i have modified the code to handle it with try and catch block. please try with below code.

        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");          
        Date currentDate = new Date();

        Log.i("Current Date",dateFormat.format(currentDate));

        try {
            Date termDate = dateFormat.parse("11/12/2017");
            System.out.println(" date ["+termDate+"]");
        }
        catch (ParseException ex){
            Log.i("Current Date","error occurred while parsing date");
        }
over 4 years ago · Santiago Trujillo Relatório

0

You need to try/catch the ParseException. Use:

   try {
        Date termDate = dateFormat.parse("11/12/2017"); 
    } catch (ParseException e) {
        e.printStackTrace();
    }

Android studio suggested me this solution. Not sure, what IDE you are using.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda