I want generate unique code in two format for my record in table. like following way
1) "Firstprefix"+"ddmmyyyy"+Sr.number e.g AD190620151
2) "secondprefix"+"ddmmyyyy"+Sr.number e.g AP190620151
if another record inserts it will be one of them but serial no change e.g AD190620152 AP190620152 this generated according to another field in record how to do this please give mi answer.
i have try in following way
@Override
public String createOrder(OrderMasterTbl order) {
getHibernateTemplate().saveOrUpdate(order);
Integer addId=order.getAdType().getId();
String id = null ;
if(addId.equals(1))
{
id="ap"+new SimpleDateFormat("ddmmyyyy").format(newDate())+order.getId();
}else{
id="dp"+new SimpleDateFormat("ddmmyyyy").
format(newDate())+order.getId();
}
System.out.print(id);
order.setOrderCode(id);
getHibernateTemplate().saveOrUpdate(order);
return id;
}
But in this way i am not able to generate proper serial number according two format .
You don't get a proper serial number here because you are using a wrong format for
SimpleDateFormat
, you are referring to month withm
while it should beM
, take a look at the SimpleDateFormat Documentation for further information.Your code should be like this:
EDIT:
In order to make the code increment in the last you have just to use two variables
ap
anddp
in your class that you initialize with1
and in your method you increment them according to yourif block
, your code should be like this: