...
// Test to change datetime items in text form
SimpleDateFormat sdfIn = new SimpleDateFormat( "dd.mm.yyyy" );
SimpleDateFormat sdfOut = new SimpleDateFormat( "yyyy.mm.dd" );
String strIn = "25.11.2023";
Date dt = sdfIn.parse( strIn );
String strOut = sdfOut.format( dt );
System.out.printf( "+++ Convert date format: '%s' -> '%s'%n", strIn, strOut );
...
public static final double SQRT_3 = Math.sqrt( 3.0d );
/**
* Calculates distance in RGB color space with Euklidian distance between each component
*
* @param c1 {@link Color} one
* @param c2 {@link Color} two
* @return double result of comparison. Max possible distance is 255.0. Minimum 0.0
*/
public static double colorDistance( Color c1, Color c2 )
{
double rDist = c1.getRed() - c2.getRed();
double gDist = c1.getGreen() - c2.getGreen();
double bDist = c1.getBlue() - c2.getBlue();
double distance = Math.sqrt( rDist * rDist + gDist * gDist + bDist * bDist ) / SQRT_3;
return distance;
}//end color distance method
Runtime rt = Runtime.getRuntime();
long usedMB = (rt.totalMemory() - rt.freeMemory()) / 1024 / 1024;
System.out.printf("Usage mamory %d Mb%n", usedMB);
/**
* Sets the current system look and feel properties
*/
public static void setSystemLookAndFeel() {
/* Get the default operation system style */
final String uiName = UIManager.getSystemLookAndFeelClassName();
/* Set this default current style for our task */
try {
UIManager.setLookAndFeel( uiName );
} catch ( Exception ex ) {
System.err.printf( "--- Can't set GUI default system style (%s):%s%n", uiName, ex.getMessage() );
}
}
package su.common.crypto;
import org.apache.commons.lang.StringUtils;
import su.common.misc.Text;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import java.security.NoSuchAlgorithmException;
/**
* Created in su.common.crypto.<br>
* User: ******<br>
* Date: 30.08.2011<br>
* Time: 17:12:45<br>
*/
public class SimpleEncDec {
private static final String DEFAULT_ALGO_NAME = "AES";
// private static final String DEFAULT_CIPHER_NAME = "AES/ECB/PKCS5Padding";
private static final String DEFAULT_CIPHER_NAME = "AES/ECB/NoPadding";
private KeyGenerator m_keygen;
private SecretKey m_key = m_keygen.generateKey();
private Cipher m_cipherIn;
private Cipher m_cipherOut;
public SimpleEncDec( String algorithmName ) throws NoSuchAlgorithmException {
// KeyGenerator m_keygen;
if ( Text.isEmpty( algorithmName ) )
m_keygen = KeyGenerator.getInstance( DEFAULT_ALGO_NAME );
else
m_keygen = KeyGenerator.getInstance( algorithmName );
// Create the cipher
try {
m_cipherIn = Cipher.getInstance( DEFAULT_CIPHER_NAME );
m_cipherIn.init( Cipher.ENCRYPT_MODE, m_key );
m_cipherOut = Cipher.getInstance( DEFAULT_CIPHER_NAME );
m_cipherOut.init( Cipher.DECRYPT_MODE, m_key );
} catch ( Exception e ) {
e.printStackTrace();
}
}
public SimpleEncDec() throws NoSuchAlgorithmException, NoSuchPaddingException {
this( null );
}
public String encrypt( String arg, String password ) {
try {
byte[] res = m_cipherIn.doFinal( arg.getBytes() );
return Base64.encode( res );
} catch ( Exception e ) {
System.err.println(e);
}
return null;
}
public String decrypt( String arg, String password ) {
byte[] res = Base64.decode( arg );
try {
res = m_cipherOut.doFinal( res );
return new String( res );
} catch ( Exception e ) {
System.err.println(e);
}
return null;
}
public static String strPad16( String strToPad ) {
final int len = strToPad.length();
if (len % 16 == 0)
return strToPad;
return StringUtils.rightPad( strToPad, (len + 16 - (len%16)) );
}
public static void main( String[] args ) throws NoSuchAlgorithmException {
SimpleEncDec sed = new SimpleEncDec( "AES" );
// check functionality
final String password = "simple_password";
String checkStr = "String to encode/decode for algorithms test";
System.out.println( String.format( "String to encrypt: \"%s\"", strPad16(checkStr) ) );
// encode string
String encStr = sed.encrypt( strPad16(checkStr), password );
System.out.println(String.format( "Encrypted into \"%s\"", encStr ) );
final String decrypt = sed.decrypt( encStr, password );
String decStr = Text.trimRight( decrypt );
System.out.println( String.format( "Decrypted to \"%s\"", decStr ) );
if ( decStr.equals( checkStr ) )
System.out.println( "+++ Encryption/decryption works well +++" );
else
System.err.println( "--- Encryption/decryption doesn't work ---" );
}
}