1

I'm trying to parse 261107 as date formatted in ddmmyy

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

public class parse {

  public static void main(String[] args) throws ParseException {
    // TODO Auto-generated method stub
    String strRawData =  "A2611070830151439.5935N 102057.0442E6.68152ID=AMY123";
    String strCode = strRawData.substring(0, 1);
    String datetemp = strRawData.substring(1, 7);
    SimpleDateFormat formatter = new SimpleDateFormat("ddmmyy");
    Date datelog = formatter.parse(datetemp);

    System.out.println(strCode);
    System.out.println(datetemp);
    System.out.println(datelog);

   }

 }

this code returns:

A
261107
Fri Jan 26 00:11:00 SGT 2007
Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
FatalError
  • 219
  • 5
  • 20
  • 1
    Hint: `mm` is minutes, not months... read http://codeblog.jonskeet.uk/2015/05/05/common-mistakes-in-datetime-formatting-and-parsing/ – Jon Skeet Aug 03 '15 at 14:23
  • also - Parsing The one-argument parse(CharSequence) method in the LocalDate class uses the ISO_LOCAL_DATE formatter. To specify a different formatter, you can use the two-argument parse(CharSequence, DateTimeFormatter) method. The following example uses the predefined BASIC_ISO_DATE formatter, which uses the format 19590709 for July 9, 1959. from https://docs.oracle.com/javase/tutorial/datetime/iso/format.html – Neil Aug 03 '15 at 14:28

4 Answers4

3

dd means day in the month

MM means month (you are using mm which means minute)

yy means year

A small modification to your SimpleDateFormatter yields:

  SimpleDateFormat dt = new SimpleDateFormat("ddMMyy");

Here is a cheatsheet from Change date format in a Java string

G   Era designator  Text    AD
y   Year    Year    1996; 96
Y   Week year   Year    2009; 09
M   Month in year   Month   July; Jul; 07
w   Week in year    Number  27
W   Week in month   Number  2
D   Day in year Number  189
d   Day in month    Number  10
F   Day of week in month    Number  2
E   Day name in week    Text    Tuesday; Tue
u   Day number of week (1 = Monday, ..., 7 = Sunday)    Number  1
a   Am/pm marker    Text    PM
H   Hour in day (0-23)  Number  0
k   Hour in day (1-24)  Number  24
K   Hour in am/pm (0-11)    Number  0
h   Hour in am/pm (1-12)    Number  12
m   Minute in hour  Number  30
s   Second in minute    Number  55
S   Millisecond Number  978
z   Time zone   General time zone   Pacific Standard Time; PST; GMT-08:00
Z   Time zone   RFC 822 time zone   -0800
X   Time zone   ISO 8601 time zone  -08; -0800; -08:00

If you would like to format the output of the date you can use another SimpleDateFormatter and the .format(Date) method.

Here is in example of how you can do this:

public static void main(String[] args) throws ParseException {

    String strRawData =  "261107";
    SimpleDateFormat dateParser = new SimpleDateFormat("ddMMyy"); //formatter for parsing date
    SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MM-yyyy"); //formatter for formatting date output
    Date date = dateParser.parse(strRawData);

    System.out.println(dateFormatter.format(date));
}

26-11-2007

Community
  • 1
  • 1
Blake Yarbrough
  • 2,286
  • 1
  • 20
  • 36
2

You have a problem in your SimpleDateFormat.

You must use the capital M for months:

SimpleDateFormat formatter = new SimpleDateFormat("ddMMyy");

See the SimpleDateFormat table provided by Java:

enter image description here

darijan
  • 9,725
  • 25
  • 38
2

Use this SimpleDateFormat: SimpleDateFormat formatter = new SimpleDateFormat("ddMMyy");

bryce
  • 842
  • 11
  • 35
1

You need to use MM not mm for months. Uppercase is months; lowercase is minutes.