Transformation String on Date



  • It's not possible to transform String on Date

    Guided by http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html I've been able to change the date where days/months/years are given in numbers, but when, for example, the name of the month I put in letters, there's a mistake.

    Works:

    SimpleDateFormat formatter = new SimpleDateFormat("dd MM yyyy");//задаю формат даты
    String dateInString = "29 11 2015";//создаю строку по заданному формату
    Date date = formatter.parse(dateInString);//создаю дату через 
    System.out.println(formatter.format(date));
    

    Not working:

    SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
    String dateInString = "28-Nov-2015";
    Date date = formatter.parse(dateInString);
    System.out.println(formatter.format(date));
    

    Issues:

    Exception in thread "main" java.text.ParseException: Unparseable date: "28-Nov-2015"
    


  • Java's trying to use regional building from the system. It's probably Russian. For the password to understand the English names of months, it needs to be created a little differently:

    new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
    

    Or write the names of months in Russian:

    String dateInString = "28-Ноя-2015";
    

Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2