-4

How to Convert String into Integer

I have a String like String date is equal "2020-03-18" , I want to convert this String into int .

Filburt
  • 17,626
  • 12
  • 64
  • 115
Anil
  • 1
  • 3

4 Answers4

2

may be if you want convert string date to int :

try {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date parsedDate = dateFormat.parse(yourDateString);
    return parsedDate.getTime();
} catch(Exception e) { //this generic but you can control another types of exception
    // look the origin of excption 
}
1pulsif
  • 471
  • 4
  • 14
0

If you need to convert this String to Date:

String date="2020-03-18";  
Date date1=new SimpleDateFormat("yyyy-MM-dd").parse(date);  
System.out.println("Date: "+date1); 

From this date you have other methods that you could get data from this Date Object ex:

date.getYear()  //would reuturn 2020 - 1900 - Year
date.getMonth() // which counts from 0 to 11 - January is 0 example
date1.getDate() // would return 18

Full code:

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author besartm
 */
public class StringToDate {
    public static void main(String[] args) {
        String date="2020-03-18";  
        Date date1;  
        try {
            date1 = new SimpleDateFormat("yyyy-MM-dd").parse(date);
            System.out.println("Date: "+date1); 
            System.out.println("Year: "+date1.getYear()); 
            System.out.println("Month: "+date1.getMonth()); 
            System.out.println("Day: "+date1.getDate()); 
        } catch (Exception ex) {

        }
    }
}
besartm
  • 558
  • 1
  • 7
  • 14
0

If you want to convert String into an int:

int num = Integer.parseInt("2018");

If you want to convert String into Date:

import java.text.SimpleDateFormat;  
import java.util.Date;
public class HelloWorld {
   public static void main(String[] args)throws Exception {  
      String date = "2020-03-18";  
      Date date1 = new SimpleDateFormat("yyyy-MM-dd").parse(date);  
      System.out.println(date1);
   }  
}  
sanitizedUser
  • 1,723
  • 3
  • 18
  • 33
0

I post here example you can run and try for yourself. I converted the input string to Date object and then to number of milliseconds. However int type is not large enough to hold that value, so I advise to use long.

import java.text.SimpleDateFormat;  
import java.util.Date;

public class DateToInt
{
    public static void main(String []args) throws Exception
    {
        String dateString = "2020-03-18";  
        Date dateObject = new SimpleDateFormat("yyyy-MM-dd").parse(dateString);
        System.out.println("date: " + dateObject);

        // int cannot hold that value, overflow happens
        int int_millis = (int) dateObject.getTime();

        // long type is better for that
        long long_millis = dateObject.getTime();

        System.out.println("int: " + int_millis);
        System.out.println("long: " + long_millis);
    }
}
sanitizedUser
  • 1,723
  • 3
  • 18
  • 33
  • Why are you using the old legacy SimpleDateFormat instead of DateTimeFormatter from java.time? – Joakim Danielson Mar 18 '20 at 08:39
  • @JoakimDanielson I don't see how that would make it better. I already have parsed the string to Date in one line. – sanitizedUser Mar 18 '20 at 08:57
  • It's about not using old deprecated classes when better ones are available. – Joakim Danielson Mar 18 '20 at 09:01
  • @JoakimDanielson Can you please post a solution using DateTimeFormatter? I found some shenanigans using LocalDate (which has to have appended time at start of day, because it's not given and also it is not default zoned -> just more problems). – sanitizedUser Mar 18 '20 at 09:09
  • You can use it like this, `LocalDate date = LocalDate.parse("2020-03-18", DateTimeFormatter.ISO_LOCAL_DATE);` and you could use `toEpochDay()` to convert that date to a long – Joakim Danielson Mar 18 '20 at 09:21
  • As I said there is a problem. It doesn't have a `getTime()` method or some equivalent. To get time you would have to apply an `epoch` and make it `ZonedDateTime`. Look at this [question](https://stackoverflow.com/questions/23944370/how-to-get-milliseconds-from-localdatetime-in-java-8). – sanitizedUser Mar 18 '20 at 09:27
  • There is no time in the input so why add it to the output and besides how do you know this is what OP is after? Maybe OP wants the int to be 20200318, no one knows. – Joakim Danielson Mar 18 '20 at 09:31